sklearn网格搜索与分组K fold cv generator

时间:2017-03-17 14:14:26

标签: scikit-learn cross-validation

我正在尝试使用随机搜索和分组k折叠交叉验证生成器在sklearn中实现对参数的网格搜索。以下作品:

skf=StratifiedKFold(n_splits=5,shuffle=True,random_state=0)
rs=sklearn.model_selection.RandomizedSearchCV(clf,parameters,scoring='roc_auc',cv=skf,n_iter=10)
rs.fit(X,y)

这不是

gkf=GroupKFold(n_splits=5)
rs=sklearn.model_selection.RandomizedSearchCV(clf,parameters,scoring='roc_auc',cv=gkf,n_iter=10)
rs.fit(X,y)

#ValueError: The groups parameter should not be None

如何指示groups参数?

这也不是

gkf=GroupKFold(n_splits=5)
fv = gkf.split(X, y, groups=groups)
rs=sklearn.model_selection.RandomizedSearchCV(clf,parameters,scoring='roc_auc',cv=gkf,n_iter=10)
rs.fit(X,y)

#TypeError: object of type 'generator' has no len()

1 个答案:

答案 0 :(得分:4)

作为参考,这是通过

完成的
rs.fit(X,y,groups=groups)

代表

rs=sklearn.model_selection.RandomizedSearchCV(forest,parameters,scoring='roc_auc',cv=gkf,n_iter=10)