Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error in coefficients for a polynomial using Numpy

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.

More info: http://docs.scipy.org/doc/numpy/reference/generated/numpy.polynomial.polynomial.Polynomial.html#numpy.polynomial.polynomial.Polynomial

like image 552
Ruips Avatar asked Jul 02 '26 20:07

Ruips


1 Answers

The Polynomial coefficients are for scaled and offset polynomials for improved numerical stability. You can either convert to a "normal" polynomial, or use the series directly if you substitute off + scl*x for x, where off and scl are returned by pol.mapparms. To convert to standard form (not recomended), do pol.convert(domain=[-1, 1]).

like image 117
Charles Harris Avatar answered Jul 05 '26 11:07

Charles Harris