More robust cross-validation estimates
Estimate model performance with less dependence on one fold assignment.
Scikit-Learn · Function
RepeatedKFold repeats K-fold cross-validation several times, randomizing the fold assignments for each repetition. Use random_state when reproducible splits are needed.
RepeatedKFold creates ordinary K-fold train/test partitions multiple times, using a different randomized arrangement for each repetition. Calling split returns index pairs for the training and test portions; setting random_state makes those generated partitions reproducible.
class sklearn.model_selection.RepeatedKFold(*, n_splits=5, n_repeats=10, random_state=None)| Name | Type | Description |
|---|---|---|
n_splits | int | Number of folds; must be at least 2. Default: 5. |
n_repeats | int | Number of times the cross-validator is repeated. Default: 10. |
random_state | int, RandomState instance or None | Controls randomization; accepts an int, RandomState instance, or None. Default: None. |
A cross-validator whose split method yields training and test indices for repeated K-fold partitions.
import numpy as np
from sklearn.model_selection import RepeatedKFold
X = np.arange(12).reshape(6, 2)
y = np.array([0, 1, 0, 1, 0, 1])
rkf = RepeatedKFold(n_splits=3, n_repeats=2, random_state=42)
for train_idx, test_idx in rkf.split(X, y):
print(train_idx, test_idx)Creates six splits: three folds for each of two randomized repetitions.
Estimate model performance with less dependence on one fold assignment.
Generate multiple randomized train/test index partitions for repeated evaluation.
Compare models under the same deterministic split sequence.
It is a cross-validator that repeats K-fold cross-validation n_repeats times. Each repetition uses a different randomization, and split yields training and test indices.
Follow-up: How do you make the generated partitions repeatable? Set random_state to an integer.
n_splits controls the number of folds in each repetition, while n_repeats controls how many complete K-fold runs are performed. The default values are 5 and 10, respectively.
Follow-up: What constraint applies to n_splits? It must be at least 2.
random_state controls the randomness used for each repeated cross-validation instance. Passing an integer produces reproducible output across multiple calls.
Follow-up: What happens when random_state is left as None? Results may differ between calls to split.
KFoldRepeatedStratifiedKFold