1

I'm trying to preform a poly fit of roughly parabolic data. I run the following line:

fit = np.polynomial.polynomial.Polynomial.fit(x, y, 2)
fit

which produces the output:

 ↦ 300.76 − 2.38(-5.67+33.36) + 4.84(-5.67+33.36)2

I'm interested in a polynomial of the form: y(x) = a + bx + cx**2. I realize that in this case:

a = 300.76  
b = -2.38  
c = 4.84

However I can't access these numbers by array indices by doing something like fit[0] or fit[1] or fit[2]. What am I missing here?

Ian Gullett
  • 107
  • 9

3 Answers3

4

Try with fit.coef, which is an array with the coefficients. So fit.coef[0], fit.coef[1] and so on.

scrx2
  • 2,242
  • 1
  • 12
  • 17
  • I think this answer is technically not correct. From the [NumPy docu](https://numpy.org/doc/stable/reference/routines.polynomials.html): _Note that the coefficients are given in the scaled domain defined by the linear mapping between the window and domain. convert can be used to get the coefficients in the unscaled data domain._ I think [this answer](https://stackoverflow.com/a/67728370/6722019) would also be the correct answer here. – M. Bernhardt Mar 30 '22 at 12:14
1

Use numpy.polynomial.polynomial.polyfit instead - returns the coefficients directly as a numpy array. The arguments passed to this function are still the same.

amzon-ex
  • 1,645
  • 1
  • 6
  • 28
0

Check the documentation

z = np.polyfit(x, y, 3)
p = np.poly1d(z)
p(0.5)

z[0],z[1] etc. is what you need. You can use the variable p to plug in any number to the polynom.

Prefect
  • 1,719
  • 1
  • 7
  • 16