我敢肯定,他們在談論什麼RepeatedStratifiedKFold
。你有兩個簡單的方法來創建20次5次。
方法1:
對於你的情況,n_splits=5, n_repeats=20
。下面的代碼只是來自scikit-learn網站的示例。
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:
可以達到與循環同樣的效果。請注意,random_state
不能是一個固定的數字,否則您將獲得20次相同的5次摺疊。
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
能夠以不同的方式(使用不同的培訓驗證比例)創建相同數量的摺疊。 除此之外,我不確定是否有其他目標。
http://scikit-learn.org/stable/modules/generated/sklearn.model_selection.RepeatedStratifiedKFold.html
謝謝RepeatedStratifiedKFold
。
是的,你可以使用它。此代碼片段與@Abhishek Thakur的答案具有相同的效果。 –