简体   繁体   中英

AttributeError: 'numpy.ndarray' object has no attribute 'powers_'

Here is my program using sklearn.

 X = np.array([[1, 2, 4],[2, 3, 9]]).T    
 print(X)
 y = np.array([1, 4, 16])
 X_poly = PolynomialFeatures(degree=2).fit_transform(X)
 print(X_poly)
 model = LinearRegression(fit_intercept = False)
 model.fit(X_poly,y)
 print('Coefficients: \n', model.coef_)
 print('Others: \n', model.intercept_)
 print(X_poly.powers_)
 X_predict = np.array([[3,3]])
 print(model.predict(feats.transform(X_predict)))

I have these errors:

 ---> 17 print(X_poly.powers_)
 18 
 19 X_predict = np.array([[3,3]])

 AttributeError: 'numpy.ndarray' object has no attribute 'powers_'

Any help please ?

Splitting X_poly into two lines should do the trick here:

X_poly_temp = PolynomialFeatures(degree=2)
X_poly = X_poly_temp.fit_transform(X)
print(X_poly_temp.powers_)

Try modifying the line

print(X_poly.powers_)

To

print(X_poly[0].powers_)

If the error disappears then loop through X_poly and print the value for each index of the array.

Split fitting into two lines, one for initializing other for fitting, also save the return value of the fit step so you can use it to train the LinearRegression model.

X_poly = PolynomialFeatures(degree=2)
X_poly_return = X_poly.fit_transform(X)
print(X_poly)
model = LinearRegression(fit_intercept = False)
model.fit(X_poly_return,y)
print('Coefficients: \n', model.coef_)
print('Others: \n', model.intercept_)
print(X_poly.powers_)
X_predict = np.array([[3,3]])

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