Ian Gullett
Ian Gullett

Reputation: 137

How to access the outputs of Numpy Polynomial.fit?

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?

Upvotes: 2

Views: 1947

Answers (3)

Prefect
Prefect

Reputation: 1777

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.

Upvotes: 0

amzon-ex
amzon-ex

Reputation: 1744

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

Upvotes: 1

scrx2
scrx2

Reputation: 2282

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

Upvotes: 4

Related Questions