【发布时间】: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