Home > OS >  np.polyval to np.polynomial.polynomial.polyval
np.polyval to np.polynomial.polynomial.polyval

Time:06-06

I don't understand how I can migrate np.polyval to np.polynomial.polynomial.polyval. Because of the reversal of coefficient order, I account for this with [::-1] and there still is an issue plotting.

1- Creating a function to plot later

def f(x):
    return np.sin(x)   0.5 * x

x = np.linspace(-2 * np.pi, 2 * np.pi, 50)

Using old method, it works:

old_polyfit =np.polyfit(x, f(x), 1)
old_polyval = np.polyval(old_polyfit , x)

plt.plot(x, f(x), 'b', label='f(x)')
plt.plot(x, old_polyval, 'r.', label='regression')

My problem: I use the new method and there are two issues,

1-

new_polyfit =np.polynomial.polynomial.polyfit(x, f(x), 1)
new_polyval = np.polynomial.polynomial.polyval(new_polyfit[::-1], x)

I see that New_polfit does not math the inverse of old_polyfit, which it should from coef order change..

2- Plotting with the new_polyval = np.polynomial.polynomial.polyval(new_polyfit[::-1], x)

error --> 1) plt.plot(x, new_polyval, 'r.', label='regression') 2) x and y must have same first dimension, but have shapes (50,) and (2,)

plt.plot(x, f(x), 'b', label='f(x)')
plt.plot(x, new_polyval, 'r.', label='regression')

I don't understand why the old_polyval would size (50,) while newpolyval is (2,).

CodePudding user response:

The new poly packages not only reverses the order of the coefficients, it also reverses the order of the arguments. Once you started to get errors, did you go back and read the docs?

your function:

In [305]: def f(x):
     ...:     return np.sin(x)   0.5 * x
     ...: 
     ...: x = np.linspace(-2 * np.pi, 2 * np.pi, 50)

the old fit:

In [308]: old_polyfit =np.polyfit(x, f(x), 1)
     ...: old_polyval = np.polyval(old_polyfit , x)

In [309]: old_polyfit
Out[309]: array([ 4.28841952e-01, -7.45793918e-17])

In [310]: old_polyval
Out[310]: 
array([-2.69449345, -2.58451412, -2.4745348 , -2.36455548, -2.25457615,
       -2.14459683, -2.0346175 , -1.92463818, -1.81465885, -1.70467953,
       -1.5947002 , ...,  2.14459683,
        2.25457615,  2.36455548,  2.4745348 ,  2.58451412,  2.69449345])

Your new try:

In [311]: new_polyfit =np.polynomial.polynomial.polyfit(x, f(x), 1)
     ...: new_polyval = np.polynomial.polynomial.polyval(new_polyfit[::-1], x)

In [312]: new_polyfit
Out[312]: array([-1.88411095e-16,  4.28841952e-01])

In [313]: new_polyval
Out[313]: array([-10.66365141,  -6.28318531])

The switch in the coefficients order is clear; why the switch? That's for the developers to explain.

Reading the docs I see the new polyval expects the (x,c) order:

In [317]: 
     ...: new_polyval = np.polynomial.polynomial.polyval(x, new_polyfit)

In [318]: new_polyval
Out[318]: 
array([-2.69449345, -2.58451412, -2.4745348 , -2.36455548, -2.25457615,
       -2.14459683, -2.0346175 , -1.92463818, -1.81465885, -1.70467953,
       -1.5947002 , ...  2.14459683,
        2.25457615,  2.36455548,  2.4745348 ,  2.58451412,  2.69449345])

these match the old.

We could also use the newfit values with the old val, by just reversing the coeff.

In [320]: old_polyval = np.polyval(new_polyfit[::-1] , x)

In [321]: old_polyval
Out[321]: 
array([-2.69449345, -2.58451412, -2.4745348 , -2.36455548, -2.25457615,
       -2.14459683, -2.0346175 , -1.92463818, -1.81465885, -1.70467953,
       -1.5947002 , ... 2.14459683,
        2.25457615,  2.36455548,  2.4745348 ,  2.58451412,  2.69449345])
  • Related