简体   繁体   中英

Polynomial Regression using sklearn

Here is my code:

import numpy as np
from sklearn.preprocessing import PolynomialFeatures

X=np.array([[1, 2, 4]]).T
print(X)
y=np.array([1, 4, 16])
print(y)
model = PolynomialFeatures(degree=2)
model.fit(X,y)
print('Coefficients: \n', model.coef_)
print('Others: \n', model.intercept_)

#X_predict=np.array([[3]])
#model.predict(X_predict)

I have these errors:

https://imgur.com/a/GkYFQlc

PolynomialFeatures doesn't have a variable named coef_ . PolynomialFeatures doesn't do a polynomial fit, it just transforms your initial variables to higher order. The full code for actually doing the regression would be:

import numpy as np
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression
from sklearn.pipeline import make_pipeline

X=np.array([[1, 2, 4]]).T
print(X)
y=np.array([1, 4, 16])
print(y)
model = make_pipeline(PolynomialFeatures(degree=2), LinearRegression(fit_intercept = False))
model.fit(X,y)
X_predict = np.array([[3]])
print(model.named_steps.linearregression.coef_)
print(model.predict(X_predict))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM