我正在尝试在Python上使用xgboost。
这是我的代码。 xgb.train
有效,但我遇到了xgb.cv
错误尽管似乎我使用了正确的方法。
以下为我工作:
###### XGBOOST ######
import datetime
startTime = datetime.datetime.now()
import xgboost as xgb
data_train = np.array(traindata.drop('Category',axis=1))
labels_train = np.array(traindata['Category'].cat.codes)
data_valid = np.array(validdata.drop('Category',axis=1))
labels_valid = np.array(validdata['Category'].astype('category').cat.codes)
weights_train = np.ones(len(labels_train))
weights_valid = np.ones(len(labels_valid ))
dtrain = xgb.DMatrix( data_train, label=labels_train,weight = weights_train)
dvalid = xgb.DMatrix( data_valid , label=labels_valid ,weight = weights_valid )
param = {'bst:max_depth':5, 'bst:eta':0.05, # eta [default=0.3]
#'min_child_weight':1,'gamma':0,'subsample':1,'colsample_bytree':1,'scale_pos_weight':0, # default
# max_delta_step:0 # default
'min_child_weight':5,'scale_pos_weight':0, 'max_delta_step':2,
'subsample':0.8,'colsample_bytree':0.8,
'silent':1, 'objective':'multi:softprob' }
param['nthread'] = 4
param['eval_metric'] = 'mlogloss'
param['lambda'] = 2
param['num_class']=39
evallist = [(dtrain,'train'),(dvalid,'eval')] # if there is a validation set
# evallist = [(dtrain,'train')] # if there is no validation set
plst = param.items()
plst += [('ams@0','eval_metric')]
num_round = 100
bst = xgb.train( plst, dtrain, num_round, evallist,early_stopping_rounds=5 ) # early_stopping_rounds=10 # when there is a validation set
# bst.res=xgb.cv(plst,dtrain,num_round,nfold = 5,evallist,early_stopping_rounds=5)
bst.save_model('0001.model')
# dump model
bst.dump_model('dump.raw.txt')
# dump model with feature map
# bst.dump_model('dump.raw.txt','featmap.txt')
x = datetime.datetime.now() - startTime
print(x)
但是如果我换行:
bst = xgb.train( plst, dtrain, num_round, evallist,early_stopping_rounds=5 )
对此:
bst.res=xgb.cv(plst,dtrain,num_round,nfold = 5,evallist,early_stopping_rounds=5)
我收到以下意外错误:
File "<ipython-input-46-ebdf0546f464>", line 45 bst.res=xgb.cv(plst,dtrain,num_round,nfold = 5,evallist,early_stopping_rounds=5) SyntaxError: non-keyword arg after keyword arg
编辑:遵循@martineau的以下建议,然后尝试此操作
bst.res=xgb.cv(plst,dtrain,num_round,evallist,nfold = 5,early_stopping_rounds=5)
产生此错误
TypeError Traceback(最近的呼叫最后)在()43#bst = xgb.train(plst,dtrain,num_round,evallist,early_stopping_rounds = 5)#early_stopping_rounds = 10#何时有一个验证集44---> 45 bst.res = xgb.cv(plst,dtrain,num_round,evallist,nfold = 5,early_stopping_rounds = 5)4647 bst.save_model('0001.model')
TypeError:cv()为关键字参数'nfold'获得了多个值
您不能在evallist
中使用cv
。因此,您应该从evallist
调用的参数中删除xgb.cv
。换句话说,您应该尝试:
bst.res = xgb.cv(plst, dtrain, num_round, nfold=5, early_stopping_rounds=5)
代替
bst.res=xgb.cv(plst,dtrain,num_round,nfold = 5,evallist,early_stopping_rounds=5)
克里斯,python培训API在pip版本和github中的当前master分支之间略有变化。他们主要在verbose_eval
函数中添加了关键字args callbacks
,folds
和cv
。 pip版本中已经存在verbose_eval
和callbacks
关键字用于train
功能,但没有用于cv
功能。
我的理解是,此错误是由于通过pip安装xgboost而导致的,而现在已过时。 XGBoost应该改为如下安装:
git clone --recursive https://github.com/dmlc/xgboost
cd xgboost; make -j4
cd python-package; sudo python setup.py install