使用xgboost算法预测值

2024-06-13 18:24:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我用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的建议后

^{pr2}$

Tags: ofimportformatmodelloadbostonpdbruntime