kfold cross validation中如何获取每一次fold的train和test数据?

How to get the train and test data for each fold in kfold cross validation?

如何访问交叉验证中每次折叠的训练和测试数据?我想将这些保存在 .csv 文件中。我尝试使用生成索引的 split 函数,但它 returns 是一个生成器对象,而不是索引。

from sklearn.model_selection import StratifiedKFold, KFold
import numpy as np
X, y = np.ones((50, 1)), np.hstack(([0] * 45, [1] * 5))
skf = StratifiedKFold(n_splits=3)
x = skf.split(X, y, groups)
x

Output:
<generator object _BaseKFold.split at 0x7ff195979580>

StratifiedKFold returns 生成器,因此您可以按如下方式对其进行迭代:

skf = StratifiedKFold(n_splits=3)
for train_index, test_index in skf.split(X, y):
     X_train, X_test = X[train_index], X[test_index]
     y_train, y_test = y[train_index], y[test_index]