Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Scikit-Learn · Class

KFold

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.

Since
0.22

How it works

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.

Syntax

class sklearn.model_selection.KFold(n_splits=5, *, shuffle=False, random_state=None)

Parameters

NameTypeDescription
n_splitsint, default=5Number of folds; must be at least 2.
shufflebool, default=FalseWhether to shuffle data before assigning samples to folds.
random_stateint, RandomState instance or None, default=NoneControls fold-index randomness when shuffle is enabled; otherwise it has no effect.

Returns

A cross-validator whose split method yields training and validation indices.

Key points

  • KFold is a cross-validator for repeatedly producing training and validation index sets.
  • Its split operation returns indices rather than transformed feature or target data.
  • The default partitioning forms consecutive folds and leaves the input order unchanged.
  • Every fold serves once as validation, with all other folds used for training in that iteration.
  • n_splits cannot be less than two.
  • An integer random_state provides reproducible shuffled splits across repeated calls.

Common mistakes

  • Setting shuffle=True randomizes the fold ordering before splitting, but does not shuffle samples inside an individual split.
  • random_state only affects results when shuffle=True; leaving shuffling disabled makes it irrelevant.

Example: Generate K-fold splits

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.

When to use it

Cross-validated evaluation

Estimate how an estimator performs across multiple held-out partitions.

Custom split pipelines

Generate index arrays for custom training and validation loops.

Shuffled validation

Reduce order-related bias when input rows have an unintended sequence.

Interview questions

QWhat does KFold do?

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.

QWhat is the role of n_splits?

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.

QHow do shuffle and random_state interact?

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.

  • StratifiedKFold
  • GroupKFold
  • RepeatedKFold
  • LeaveOneOut

Source: KFold — scikit-learn 1.9.1 documentation. Checked 2026-10-15.