我一直在尝试在python 2.7中实现这个代码。它给了我这个错误。我很感激帮助。 我有最新版本的sklearn(0.18.1)和xgboost(0.6)
import xgboost as xgb
from sklearn.model_selection import StratifiedKFold
from sklearn.metrics import f1_score, roc_auc_score, confusion_matrix
nfold = 3
kf = StratifiedKFold(nfold, shuffle=True)
dtrain = xgb.DMatrix(x_train, label=y_train)
dtest = xgb.DMatrix(x_test)
params = {
'objective' : 'binary:logistic',
'eval_metric': 'auc',
'min_child_weight':10,
'scale_pos_weight':scale,
}
hist = xgb.cv(params, dtrain, num_boost_round=10000, folds=kf, early_stopping_rounds=50, as_pandas=True, verbose_eval=100, show_stdv=True, seed=0)
我收到此错误:
TypeErrorTraceback (most recent call last)
<ipython-input-52-41c415e116d7> in <module>()
5 'scale_pos_weight':scale,
6 }
----> 7 hist = xgb.cv(params, dtrain, num_boost_round=10000, folds=kf, early_stopping_rounds=50, as_pandas=True, verbose_eval=100, show_stdv=True, seed=0)
8
9
/opt/conda/lib/python2.7/site-packages/xgboost/training.pyc in cv(params, dtrain, num_boost_round, nfold, stratified, folds, metrics, obj, feval, maximize, early_stopping_rounds, fpreproc, as_pandas, verbose_eval, show_stdv, seed, callbacks)
369
370 results = {}
--> 371 cvfolds = mknfold(dtrain, nfold, params, seed, metrics, fpreproc, stratified, folds)
372
373 # setup callbacks
/opt/conda/lib/python2.7/site-packages/xgboost/training.pyc in mknfold(dall, nfold, param, seed, evals, fpreproc, stratified, folds)
236 idset = [randidx[(i * kstep): min(len(randidx), (i + 1) * kstep)] for i in range(nfold)]
237 elif folds is not None:
--> 238 idset = [x[1] for x in folds]
239 nfold = len(idset)
240 else:
TypeError: 'StratifiedKFold' object is not iterable
答案 0 :(得分:2)
在.jar
函数内,尝试替换
xgb.cv
与
folds=kf
应用split method以便将其拆分为培训和验证。然后我们将其转换为folds=list(kf.split(x_train,y_train))
,以便它是一个可迭代的对象。
如果不起作用,请尝试不使用list
。那就是:
list
答案 1 :(得分:0)
如错误所示,kf是'StratifiedKFold'对象。
这个对象有一个.split()方法,它会为你提供一个包含不同列号/有效元素索引的生成器。
folds_generator = kf.split(x_train, y_train)
但是,请阅读xgb.cv doc,
folds:list,提供了使用预定义CV折叠列表的可能性(每个元素必须是测试折叠索引的向量)。提供折叠时,将忽略nfold和分层参数。
folds需要一个类型为'list'的参数。您可以使用以下代码将生成器转换为列表
folds_list = list(folds_generator)