【问题标题】:Unable to make Prediction on Test Data无法对测试数据进行预测
【发布时间】:2020-08-29 05:17:55
【问题描述】:
#Linear Regression Model
from sklearn import linear_model
linear_model = linear_model.LinearRegression()
linear_model.fit(x_train, y_train)
print("Linear Model Coefficients:", linear_model.coef_)
print("Linear Model Intercepts:", linear_model.intercept_)
print("")
#Predicting Prices using the models
y_pred = linear_model.predict(x_test)

但这给出了一个输出:module 'sklearn.linear_model' has no attribute 'predict'

【问题讨论】:

  • 这不仅仅是一个输出,而是一个例外。请发布完整的回溯。
  • 解决了。模型变量名称和模型名称相似,这就是它出现错误的原因。

标签: python scikit-learn linear-regression sklearn-pandas


【解决方案1】:

使用其他变量名代替linear_model。变量和函数名称相等,导致您遇到的错误。

示例代码如下。

from sklearn import linear_model
model = linear_model.LinearRegression()
model.fit(x_train, y_train)
print("Linear Model Coefficients:", model.coef_)
print("Linear Model Intercepts:", model.intercept_)
print("")
#Predicting Prices using the models
y_pred = model.predict(x_test)

【讨论】:

  • 非常感谢 :) 像魔术一样工作。奇怪的是,它访问 coef_ 和 intercept_ 对象没有问题,但不能调用该名称的函数。
猜你喜欢
  • 1970-01-01
  • 2021-03-06
  • 2020-12-07
  • 2020-12-08
  • 2020-09-06
  • 2019-12-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多