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:
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))