python - Which is the simplest way to make a polynomial regression with sklearn? -
i have data doesn't fit linear regression,
in fact should fit 'exactly' quadratic function:
p = r*i**2
i'm miking this:
model = sklearn.linear_model.linearregression()
x = alambres[alambre]['mediciones'][x].reshape(-1, 1) y = alambres[alambre]['mediciones'][y].reshape(-1, 1) model.fit(x,y)
is there chance solve doing like:
model.fit([x,x**2],y)
?
you can use numpy's polyfit.
import numpy np matplotlib import pyplot plt x = np.linspace(0, 100, 50) y = 23.24 + 2.2*x + 0.24*(x**2) + 10*np.random.randn(50) #added noise coefs = np.polyfit(x, y, 2) print(coefs) p = np.poly1d(coefs) plt.plot(x, y, "bo", markersize= 2) plt.plot(x, p(x), "r-") #p(x) evaluates polynomial @ x plt.show()
out:
[ 0.24052058 2.1426103 25.59437789]
Comments
Post a Comment