【问题标题】:predict values using xgboost algorithm使用 xgboost 算法预测值
【发布时间】:2020-01-10 22:17:35
【问题描述】:

我已经使用 Xgboost 编写了波士顿房价的代码 这是代码

import treelite
import xgboost
from sklearn.datasets import load_boston
import treelite.runtime     # runtime module

X, y = load_boston(return_X_y=True)
print('dimensions of X = {}'.format(X.shape))
print('dimensions of y = {}'.format(y.shape))

dtrain = xgboost.DMatrix(X, label=y)
params = {'max_depth':3, 'eta':1, 'silent':1, 'objective':'reg:linear',
          'eval_metric':'rmse'}
bst = xgboost.train(params, dtrain, 20, [(dtrain, 'train')])

bst.save_model('bst1.model')

bst = xgboost.Booster({'nthread':4}) #init model
bst.load_model("bst1.model") # load data

#Now I want to make prediction using above model

(Pdb) bst.predict(X,10,20)
*** AttributeError: 'numpy.ndarray' object has no attribute 'feature_names'
(Pdb) bst.predict(10,20)
*** AttributeError: 'int' object has no attribute 'feature_names'
(Pdb)

对输入值 10 和 20 进行预测的正确方法是什么?

编辑

接受 Adji 的建议后

(Pdb) bst.predict([[10,20]])
*** AttributeError: 'list' object has no attribute 'feature_names'
(Pdb) bst.predict([10,20])
*** AttributeError: 'list' object has no attribute 'feature_names'

【问题讨论】:

  • 从错误消息中,您看到bst.predict 的输入必须有一个名为feature_names 的属性。即 - 它不能是 int、数组、列表或任何其他基本类型。从“开始”页面的示例中,您可以看到您需要一个 xgb.DMatrix 对象作为输入。

标签: python machine-learning prediction xgboost


【解决方案1】:

我认为你应该尝试改变

10,20 到一个矩阵,或者

[[10,20]](按照xgb.Dmatrix(...)结果的格式)

【讨论】:

  • link 检查这个链接,也许它对你有帮助。测试值与训练值格式相同,idk 为print(dtrain)的结果
  • 或查看link,也许你会找到@user2129623 的东西。我认为bst.predict(...) 需要xgb.DMatrix(...) 作为输入,上面有'feature_names'。
猜你喜欢
  • 1970-01-01
  • 2018-12-31
  • 2016-08-21
  • 2018-12-23
  • 2016-06-02
  • 1970-01-01
  • 2012-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多