AttributeError:“ numpy.ndarray”对象没有属性“ powers_”

时间:2018-08-13 10:57:21

标签: python numpy scikit-learn

这是我使用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_'

请帮忙吗?

3 个答案:

答案 0 :(得分:0)

X_poly分成两行应该可以解决此问题:

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

答案 1 :(得分:0)

尝试修改该行

print(X_poly.powers_)

收件人

print(X_poly[0].powers_)

如果错误消失,则遍历X_poly并打印数组每个索引的值。

答案 2 :(得分:0)

将拟合分为两行,一行用于初始化另一行用于拟合,还保存了拟合步骤的返回值,因此您可以使用它来训练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]])