gpt4 book ai didi

python - XGBoost:我的 xgb.cv 调用语法有什么问题?

转载 作者:太空狗 更新时间:2023-10-29 20:29:58 25 4
gpt4 key购买 nike

我正在尝试在 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 (most recent call last) in () 43 # bst = xgb.train( plst, dtrain, num_round, evallist,early_stopping_rounds=5 ) # early_stopping_rounds=10 # when there is a validation set 44 ---> 45 bst.res=xgb.cv(plst,dtrain,num_round,evallist,nfold = 5,early_stopping_rounds=5) 46 47 bst.save_model('0001.model')

TypeError: cv() got multiple values for keyword argument 'nfold'

最佳答案

您不能在cv 中使用evallist。因此,您应该从 xgb.cv 调用的参数中删除 evallist。换句话说,您应该尝试:

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 分支之间略有变化。他们主要是在cv函数中加入了关键字参数verbose_evalcallbacksfoldsverbose_evalcallbacks 关键字已经存在于 train 函数的 pip 版本中,但不是 cv 函数.

关于python - XGBoost:我的 xgb.cv 调用语法有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37648443/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com