Small-sample validation
Estimating model performance when the dataset is small and retaining nearly all observations for each training run matters.
Scikit-Learn · Function
LeaveOneOut is a cross-validator that holds out one sample at a time while using all remaining samples for training. It creates as many splits as there are samples.
LeaveOneOut generates one train/test split for every sample. In each split, exactly one sample is the test set and all other samples are used for training, so the number of splits equals the number of samples.
class sklearn.model_selection.LeaveOneOut()An iterator yielding pairs of train and test index arrays.
import numpy as np
from sklearn.model_selection import LeaveOneOut
X = np.array([[1, 2], [3, 4], [5, 6]])
y = np.array([0, 1, 1])
loo = LeaveOneOut()
for train_idx, test_idx in loo.split(X, y):
print("train:", train_idx, "test:", test_idx)Creates one split per sample and prints the training and singleton test indices.
Estimating model performance when the dataset is small and retaining nearly all observations for each training run matters.
Inspecting how predictions change when each individual observation is omitted.
Generating explicit train/test index pairs for custom evaluation loops.
LeaveOneOut is a cross-validator that creates one split for each sample. The held-out test set contains one sample, while every other sample forms the training set.
Follow-up: How many splits are produced for n samples? Exactly n splits, because each sample is held out once.
Its test sets are singletons, and the corresponding training sets contain all remaining samples. The splitter returns train and test indices rather than fitting an estimator itself.
Follow-up: What does split return? It yields pairs of index arrays identifying the training and test rows for each fold.
LeaveOneOut is equivalent to KFold with n_splits equal to the number of samples and to LeavePOut with p equal to 1. Its main drawback is the potentially high computation cost.
Follow-up: When might another strategy be preferable? For large datasets, use a cross-validation method with fewer folds to reduce repeated model fitting.
KFoldLeavePOutget_n_splitssplit