我想做什么?
我试图在 GridSearchCV()
中使用 StratifiedKFold()
。
那么,是什么让我感到困惑了?
当我们使用 K 折交叉验证时,只需在 GridSearchCV()
中传递 CV 的数量,如下所示。
grid_search_m = GridSearchCV(rdm_forest_clf, param_grid, cv=5, scoring='f1', return_train_score=True, n_jobs=2)
接下来,当我需要使用StratifiedKFold()
时,我认为流程应该保持不变。也就是说,只需设置拆分数-StratifiedKFold(n_splits=5)
为cv
。
grid_search_m = GridSearchCV(rdm_forest_clf, param_grid, cv=StratifiedKFold(n_splits=5), scoring='f1', return_train_score=True, n_jobs=2)
但是这个答案说:
此外,这个问题的一个答案也建议这样做。 这意味着,他们建议在使用whatever the cross validation strategy used, all that is needed is to provide the generator using the function split, as suggested:
kfolds = StratifiedKFold(5) clf = GridSearchCV(estimator, parameters, scoring=qwk, cv=kfolds.split(xtrain,ytrain)) clf.fit(xtrain, ytrain)
GridSearchCV()
时调用split()函数:StratifiedKFold(n_splits=5).split(xtrain,ytrain)
。 但是,我发现调用split()
和不调用split()
会给出相同的f1分数。因此,我的问题是:
我不明白为什么在分层K折交叉验证中需要调用
split()
函数,而在K折交叉验证中不需要这样做。如果调用了
split()
函数,GridSearchCV()
如何工作,因为split()
函数返回训练和测试数据集索引? 也就是说,我想知道GridSearchCV()
将如何使用这些索引?