在Python中运行xgboost的问题

时间:2018-09-06 13:31:55

标签: python xgboost

在使用Python运行xgboost时,我遇到了例外情况:

有关可重现的示例:

import sklearn
from sklearn import datasets
data, labels = sklearn.datasets.load_diabetes(return_X_y= True)
labels = (labels > 100) * 1
X_train = data[:300, :]
y_train = labels[:300]
X_test = data[300:,:]
y_test = labels[300:]
dtrain = xgb.DMatrix(data = X_train, label = np.array(y_train), feature_names =['age',
  'sex',
  'bmi',
  'bp',
  's1',
  's2',
  's3',
  's4',
  's5',
  's6'])
dtest = xgb.DMatrix(data = X_test, label = np.array(y_test), feature_names = ['age',
  'sex',
  'bmi',
  'bp',
  's1',
  's2',
  's3',
  's4',
  's5',
  's6'])
param = {'colsample_bytree': 0.7,
 'eta': 0.3,
 'gamma': 3.0,
 'max_depth': 6,
 'min_child_weight': 4.0,
 'objective': 'binary:logistic',
 'reg_alpha': 0.3,
 'reg_lambda': 0.3,
 'silent': 0,
 'subsample': 0.8}
watchlist = [(dtest, 'eval'), (dtrain, 'train')]
num_boost_round = 100
bst = xgb.train(params = param, watchlist = watchlist , dtrain = dtrain, num_boost_round = num_boost_round,   \
                    early_stopping_rounds = True)

这导致异常:

TypeError: train() got an unexpected keyword argument 'watchlist'

如果我删除监视列表,则会得到另一个异常:

bst = xgb.train(params = param, dtrain = dtrain, num_boost_round = num_boost_round,   \
                    early_stopping_rounds = True)
IndexError: list index out of range

您的建议将不胜感激。

1 个答案:

答案 0 :(得分:0)

来自文档

getView()

该参数不是监视列表,而是评估值。

只是改变一下,我得到了以下信息:

xgboost.train(params, dtrain, num_boost_round=10, evals=(), obj=None, 
feval=None, maximize=False, early_stopping_rounds=None, evals_result=None, 
verbose_eval=True, xgb_model=None, callbacks=None, learning_rates=None)

Train a booster with given parameters.

Parameters:

params (dict) – Booster params.

dtrain (DMatrix) – Data to be trained.

num_boost_round (int) – Number of boosting iterations.

evals (list of pairs (DMatrix, string)) – List of items to be evaluated during 
training, this allows user to watch performance on the validation set.