簡體   English   中英

AttributeError:“ numpy.ndarray”對象沒有屬性“ powers_”

[英]AttributeError: 'numpy.ndarray' object has no attribute 'powers_'

這是我使用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)))

我有這些錯誤:

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

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

有什么幫助嗎?

X_poly分為兩行應該可以解決問題:

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

嘗試修改線

print(X_poly.powers_)

print(X_poly[0].powers_)

如果錯誤消失,則遍歷X_poly並打印數組每個索引的值。

將擬合分為兩行,一行用於初始化另一行用於擬合,還保存了擬合步驟的返回值,因此您可以使用它來訓練LinearRegression模型。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM