How to fit a polynomial curve to data using scikit-learn?

Problem context

Using scikit-learnwith Python, I am trying to match a quadratic polynomial curve with a data set, so that the model will look like y = a2x^2 + a1x + a0, and the coefficients anwill be represented by the model,

Problem

I don’t know how to fit a polynomial curve using this package, and there seem to be surprisingly few clear references to how to do this (I have been looking for some time). I saw this question about doing something similar with NumPy , as well as this question, which makes the form more complex than I require .

What a good solution would look like

I hope a good solution goes like this (the sample is adapted from the linear matching code that I use):

x = my_x_data.reshape(len(profile), 1)
y = my_y_data.reshape(len(profile), 1)
regression = linear_model.LinearRegression(degree=2) # or PolynomialRegression(degree=2) or QuadraticRegression()
regression.fit(x, y)

, scikit-learn , (, R, , ).

:

, ?

+4
2

: https://stats.stackexchange.com/questions/58739/polynomial-regression-using-scikit-learn.

, scikit-learn? , , , numpy:

z = np.poly1d(np.polyfit(x,y,2))

z(x) x.

scikit-learn .

+7

, . scikit-learn , . , . , :

from sklearn.preprocessing import PolynomialFeatures
from sklearn import linear_model

X = [[0.44, 0.68], [0.99, 0.23]]
vector = [109.85, 155.72]
predict= [0.49, 0.18]

poly = PolynomialFeatures(degree=2)
X_ = poly.fit_transform(X)
predict_ = poly.fit_transform(predict)

clf = linear_model.LinearRegression()
clf.fit(X_, vector)
print clf.predict(predict_)
+5

Source: https://habr.com/ru/post/1608079/


All Articles