I am using the numpy.polynomial.polynomial.Polynomial class (Numpy library) in order to fit with the method fit() certain data to a polynomial function. The polynomial obtained is all right and I can plot it and substitute points in order to get the ‘y’ value, and I get correct responses. The problem is that the .coef attribute of the Polynomial class returns a set of coefficients that are somehow normalized or changed and I cant see how. What do I mean? The code follows:
x_vid = array([0.0, 50.0, 75.0, 100.0])
y_vid = array([0.0, 30.0, 55.0, 100.0])
pol = Polynomial.fit(x_vid, y_vid, 5) # The polynomial is OK!!
print pol.coef
The .coef attribute returns the next array:
30 38.16 17.93 9.98 2.06 1.85
The coefficients are in increasing order so, so those coefficients represent the following polynomial function:
30 + 38.16x + 17.93x^2 + 9.98x^3 + 2.06x^4 + 1.85x^5
However and here comes the problem, if I substitute any value from my range of values [0-100] there, it will not return the proper value, despite that if I do for example:
pol(0) → I will get a 0 which is OK, but it is immediate to see that in the polynomial I have written, it will not return 0 at x=0.
I think the polynomial function might be normalized or displaced. I might be facing here a mathematical problem here instead of a programming one, but any help is really welcome, because I need to write down the polynomial and I am not sure of the correct form of it. Thanks.
numpy.polyfit? Also, I don't get the same coefficients as you, mine are two orders of magnitude larger. - darthbithpolyfitwere reasonable and the y-intercept matched the value ofpolyval(pol, 0). I have no idea what the coefficients returned from thePolynomial.fit()method are - darthbith