我正在尝试在Python中使用XGBoost包
运行此代码时出现此错误

import xgboost as xgb
data=np.array(traindata.drop('Category',axis=1))
labels=np.array(traindata['Category'].cat.codes)

dtrain = xgb.DMatrix( data, label=labels)

param = {'bst:max_depth':6, 'bst:eta':0.5, 'silent':1, 'objective':'multi:softprob' }
param['nthread'] = 4
param['eval_metric'] = 'mlogloss'
param['lambda'] = 1
param['num_class']=39

evallist  = [(dtrain,'train')]

plst = param.items()
plst += [('eval_metric', 'ams@0')]

num_round = 10
bst = xgb.train( plst, dtrain, num_round, evallist )

bst.save_model('0001.model')



  -------------------------------------------------- ------------------------- XGBoostError错误回溯(最近的调用
  最后)在()
       17
       18 num_round = 10
  ---> 19 bst = xgb.train(plst,dtrain,num_round,evallist)
       20
       21 bst.save_model('0001.model')
  
  /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/xgboost/training.pyc
  在训练中(params,dtrain,num_boost_round,evals,obj,feval,maximum,
  early_stopping_rounds,evals_result,verbose_eval,learning_rates,
  xgb_model)
      122 nboost + = 1
      123如果len(evals)!= 0:
  -> 124 bst_eval_set = bst.eval_set(evals,i,feval)
      125如果isinstance(bst_eval_set,STRING_TYPES):
      126毫秒= bst_eval_set
  
  /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/xgboost/core.pyc
  在eval_set中(self,evals,迭代,feval)
      (753)
      754 dmats,evnames,len(evals),
  -> 755 ctypes.byref(msg)))
      756返回msg.value
      757:
  
  /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/xgboost/core.pyc
  在_check_call(ret)中
       95“”“
       96如果ret!= 0:
  ---> 97引发XGBoostError(_LIB.XGBGetLastError())
       98
       99
  
  XGBoostError:我们需要权重来评估ams


我在文档中什么都看不到

https://xgboost.readthedocs.io/en/latest/python/python_intro.html

http://www.analyticsvidhya.com/blog/2016/03/complete-guide-parameter-tuning-xgboost-with-codes-python/

最佳答案

在计算ams指标时,您需要为每个标记的训练点分配权重。在创建DMatrix时,可以通过使用关键字参数weight来设置权重。一个简单的例子。

weights = np.ones(len(labels))
dtrain = xgb.DMatrix(data, label = labels, weight = weights)


还有最近的Kaggle竞赛中的一个深入示例:https://github.com/tqchen/xgboost/blob/master/demo/kaggle-higgs/higgs-numpy.py

08-07 16:49