How can I get a value from a polynomial defined with np.poly1d?

2.4k Views Asked by At

I made a model from a series of data. My model is represented by the red line which has the following formula:

p4=np.poly1d(np.polyfit(x,y,4)) #0.04253 x - 3.593 x + 89.6 x - 470.3 x + 666.4

enter image description here How can I retrieve a value from my model (from the red polynomial line)? I tried with this code but results are not coherent:

y=np.arange(len(x))
X=scale.fit_transform(y.values)
X=np.array(X)
X.reshape(-1,1)
est = sm.OLS(y, X).fit()

scaled = scale.transform(50)
predicted = est.predict(scaled[0])

With x=50 I retrieve 1 as prediction that's obviously not coherent with the model. Could you help me?

1

There are 1 best solutions below

0
Joe On

You can get the value by using the polynomial function returned by np.poly1d.

See the example shown in the documentation:

import numpy as np

p = np.poly1d([1, 2, 3])
print(np.poly1d(p))

# Evaluate the polynomial at x = 0.5:
y = p(0.5)
print(y)