Cross-validated evaluation
Estimate how an estimator performs across multiple held-out partitions.
Scikit-Learn · Class
KFold creates k train/validation splits by holding out each fold once and training on the remaining folds. It can optionally shuffle fold assignment and make that ordering reproducible.
KFold divides sample indices into a chosen number of consecutive folds and repeatedly returns one fold as validation data while using the remaining folds for training. Shuffling is optional; when enabled, an integer random_state makes the resulting fold ordering reproducible.
class sklearn.model_selection.KFold(n_splits=5, *, shuffle=False, random_state=None)| Name | Type | Description |
|---|---|---|
n_splits | int, default=5 | Number of folds; must be at least 2. |
shuffle | bool, default=False | Whether to shuffle data before assigning samples to folds. |
random_state | int, RandomState instance or None, default=None | Controls fold-index randomness when shuffle is enabled; otherwise it has no effect. |
A cross-validator whose split method yields training and validation indices.
from sklearn.model_selection import KFold
X = [[1], [2], [3], [4], [5], [6]]
kf = KFold(n_splits=3, shuffle=True, random_state=42)
for train_index, test_index in kf.split(X):
print(train_index, test_index)Creates three shuffled, reproducible folds and prints the training and validation indices for each split.
Estimate how an estimator performs across multiple held-out partitions.
Generate index arrays for custom training and validation loops.
Reduce order-related bias when input rows have an unintended sequence.
KFold is a cross-validator that yields train/test index arrays. It partitions the data into n_splits folds and uses each fold once for validation while training on the others.
Follow-up: How would you make the splits reproducible? Enable shuffle and pass an integer random_state.
n_splits controls how many folds are created and must be at least 2. Each validation iteration uses one fold, so the number of generated train/test splits equals n_splits.
Follow-up: What happens when the sample count is not divisible by n_splits? The first n_samples % n_splits folds contain one more sample than the others.
By default, KFold creates consecutive folds without shuffling. With shuffle=True, it changes the ordering of indices used to form folds; random_state can control that randomness.
Follow-up: Does shuffle=True randomize the order within each returned train or test split? No—the documentation states that samples within each split are not shuffled.
StratifiedKFoldGroupKFoldRepeatedKFoldLeaveOneOut