我很肯定他们在谈论RepeatedStratifiedKFold。你有两个简单的方法来创建5个折叠20次。在
方法1:
对于您的情况,n_splits=5, n_repeats=20。下面的代码只是scikit学习网站的示例。在from sklearn.model_selection import RepeatedStratifiedKFold
X = np.array([[1, 2], [3, 4], [1, 2], [3, 4]])
y = np.array([0, 0, 1, 1])
rskf = RepeatedStratifiedKFold(n_splits=2, n_repeats=2,
... random_state=42)
>>> for train_index, test_index in rskf.split(X, y):
... print("TRAIN:", train_index, "TEST:", test_index)
... X_train, X_test = X[train_index], X[test_index]
... y_train, y_test = y[train_index], y[test_index]
...
TRAIN: [1 2] TEST: [0 3] # n_repeats==1: the folds are [1 2] and [0 3]
TRAIN: [0 3] TEST: [1 2]
TRAIN: [1 3] TEST: [0 2] # n_repeats==2: the folds are [1 3] and [0 2]
TRAIN: [0 2] TEST: [1 3]
方法2:You can achieve the same effect with looping. Note that the random_state cannot be a fixed number, otherwise you will get the same 5 folds for 20 times.for i in range(20):
kfold = StratifiedKFold(n_splits=5, shuffle=True, random_state=i)
为什么它和你的代码不同?
假设你有10000个数据点,你创建了100个折叠。1倍的大小=100。您的培训集=9900,而验证集=100。在
RepeatedStratifiedKFold为您的模型创建5个折叠,每个折叠为2000。然后重复做5次折叠,再重复20次。这意味着您可以实现100倍,但是有一个非常大的验证集。根据您的目标,您可能需要一个更大的验证集,例如有足够的数据来正确验证,RepeatedStratifiedKFold使您能够以不同的方式创建相同数量的折叠(使用不同的训练验证比例)。除此之外,我不确定是否还有其他目标。
谢谢RepeatedStratifiedKFold。在