Reputation: 13
Some preface, I have been teaching myself python for the past few days for a project, with almost no history of coding beyond some dabbling with MATLAB, so I apologize if there is something very obvious I have missed that can solve this problem. As of now, my code reads data from an excel sheet using pandas, which I do some processing to in order to create some x-y points. What I want is to fit those data points with a polynomial line-of-best-fit, of which the user can define the power, and use it to interpolate/extrapolate to approximate the y value at any given x value.
This is the current output of pandas and the data processing, which will be used to create points like (250.57, 662.95), (198.92, 552.55), etc:
[250.574625, 198.9212, 149.8915888888889, 98.72372222222222, 47.3914] [662.9490319500001, 552.5478768555556, 419.0056029333333, 280.815218, 142.783332425]
Beyond that, what I have so far is the following:
import numpy as np
Fy = [323,375,457,473]
Fz = [100,150,200,250]
deg = 5
rnd = 4
poly_line = np.polyfit(Fz, Fy, deg, full=False).round(rnd)
Coefs = {}
for i, item in enumerate(poly_line, start=1):
Coef_name = f"Coef{i}"
Coefs[Coef_name] = item
Here, Fy and Fz are just placeholders for the data sets from pandas that will be used as the (x,y) data points. This code correctly outputs the coefficients (a, b, c, etc.) for the equation, and I can attribute them to what power they multiply with (x^2, x, 1, etc.) by using i. However, what I'm not entirely sure about is how to reference these coefficients as an equation, like y = ax^2 + bx + c, where the power is dependent on the "deg = 5" line, and then use that equation to calculate a y value for any given x value (the most important part of this code). The ultimate goal here is to have a gui I'm designing allow the user to input some parameters to determine what set of data to use, as well as input what degree polynomial they want and what x value they want to evaluate at, resulting in the code outputting both the calculated y value and a plot containing the data points and line-of-best-fit. Being new to python (and coding, in general), I'm not sure if this kind of data processing is necessarily something it's designed to do, and if there is an alternate route I should be taking instead.
I understand there have been some other posts about defining a polynomial of varying degree, like Create a polynomial of arbitrary degree in python. But I think what differentiates my question is that I don't only want to know what the equation is, I also need to use it to perform future calculations with.
Upvotes: 0
Views: 82