Skip to content
GATEiit

GATE DA · Machine Learning

Cross-validation: leave-one-out (LOO) and k-fold

Cross-validation estimates test error by rotating which part of the data is held out: k-fold, leave-one-out (k = n), the number of fits each needs, their bias and variance, and how to use them for model selection without leakage.

Before you start

Concept notes

1. Why hold out data

Training error underestimates test error because the model has already seen the points. To estimate generalisation:

  • Hold-out (validation set) method: split once into training and validation parts (say 70/30), fit on one, measure error on the other. Simple, but the estimate depends on which points land where, and the model is trained on less data.
  • Train / validation / test: use the validation data to choose hyperparameters (k, λ, depth, C) and the test data, touched only once at the end, to report final performance. Choosing anything using the test set makes the reported test error optimistic.

Cross-validation reuses the data more efficiently: every point is used for validation exactly once and for training in the other rounds.

Data leakage is the most common CV mistake: any step that learns from data (standardisation means and variances, feature selection, imputation, PCA) must be fitted inside each training fold only, then applied to that round's validation fold. Fitting it on the whole dataset first lets validation information leak into training.

For time-ordered data, folds must respect time (train on the past, validate on the future); random shuffling would leak the future.

2. k-fold cross-validation

Procedure:

  1. Shuffle and split the n points into k roughly equal folds.
  2. For i=1,…,k: train on the other k−1 folds, compute the error Ei on fold i.
  3. Report the CV error 1k∑iEi (or the size-weighted average if folds differ in size).

Each round trains on about k−1kn points and validates on nk.

Cost: k model fits per hyperparameter setting. A grid of g settings costs g⋅k fits, plus one final refit on all the data with the chosen setting.

Stratified k-fold keeps the class proportions of the whole dataset in every fold; use it for classification, especially with imbalanced classes.

Common choices are k=5 or k=10: each model sees 80–90% of the data, so the estimate is only slightly pessimistic, and the cost stays moderate.

After choosing a hyperparameter by CV, refit on the full training data with that setting. The CV error of the winning setting is slightly optimistic because it was selected as the minimum; nested CV (an inner CV for tuning inside an outer CV for evaluation) gives an honest estimate.

data split into 5 foldsRound 1valtraintraintraintrainerror E1Round 2trainvaltraintraintrainerror E2Round 3traintrainvaltraintrainerror E3Round 4traintraintrainvaltrainerror E4Round 5traintraintraintrainvalerror E5CV error = (E1 + E2 + E3 + E4 + E5) / 5
5-fold cross-validation: each fold is the validation set once

3. Leave-one-out cross-validation (LOO)

LOO is k-fold with k=n: each round leaves out a single point, trains on the remaining n−1 and tests on the one left out. CVLOO=1n∑i=1nL(yi,f^(−i)(xi)), where f^(−i) is trained without point i.

  • Deterministic: there is no random split, so the answer is unique.
  • Nearly unbiased: each model uses n−1 points, almost the full data.
  • Higher variance as an estimate: the n training sets overlap almost completely, so the errors are highly correlated and their average does not settle as much as you might expect.
  • Expensive: n fits in general.

Shortcut for least squares: for linear regression (and ridge, with its own hat matrix) the LOO residual is yi−f^(−i)(xi)=ei1−hii, where ei is the ordinary residual and hii the leverage, so LOO costs one fit. For the mean predictor (hii=1/n) this gives LOO residuals nn−1(yi−yˉ).

LOO with kNN is cheap too: just find each point's nearest neighbours among the others.

4. Choosing k and using CV well

Hold-out k-fold (k=5,10) LOO (k=n)
Fits 1 k n
Training size e.g. 70% k−1kn n−1
Bias of estimate (pessimism) highest small lowest
Variance of estimate high (one split) moderate can be high
Randomness yes yes none

So increasing k reduces the bias of the error estimate (bigger training sets) but costs more and can increase its variance.

Using CV for model selection:

  • Compute the CV error for each candidate (each λ, k, depth, kernel) and pick the lowest. A common rule is the one-standard-error rule: choose the simplest model whose CV error is within one standard error of the best.
  • Use the same folds for all candidates so that comparisons are fair.
  • CV estimates the error of the procedure (model family + tuning) on data of this size, not of one particular fitted model.

Bootstrap is a different resampling method: draw n points with replacement; about 1−(1−1/n)n≈63.2% of distinct points appear, and the rest (out-of-bag) can be used for validation.

Formulas to remember

k-fold CV error
CV(k)=1k∑i=1kEi
LOO CV error
CVLOO=1n∑i=1nL(yi, f^(−i)(xi))
Training size per round
k−1k n
Fits for a grid searchg hyperparameter settings, k folds
g×k (+1 final refit)
LOO shortcut for least squares
yi−f^(−i)(xi)=ei1−hii
Bootstrap distinct fraction
1−(1−1n)n≈1−e−1≈0.632

Common mistakes

  • Fitting scaling, imputation or feature selection on the whole dataset before CV (data leakage).
  • Using the test set for model selection and then reporting its error as unbiased.
  • Counting fits as k in total when there are several hyperparameter values; it is g×k.
  • Saying LOO always has the lowest variance; it has the lowest bias, but its estimate can be quite variable.
  • Shuffling time-series data into random folds, which lets the model see the future.

Worked examples

Try each step yourself, then reveal the working.

Example 1: LOO error of the mean predictor

numerical

Targets y=(2,4,6,8). The model predicts the mean of its training targets. Find the LOO mean squared error.

  1. Step 1: Leave out each point and predict.

    Try it on paper first.

Example 2: LOO error of 1-NN

numerical

One-dimensional labelled points: 1 (A), 2 (A), 4 (B), 5 (B), 7 (A). Find the LOO error rate of the 1-nearest-neighbour classifier.

  1. Step 1: Classify each point using the others.

    Try it on paper first.

Example 3: Cost and fold sizes

numerical

n=500 points, 10-fold CV, and 8 candidate values of λ. How many points train each model, how many fits are needed, and how would LOO compare?

  1. Step 1: Fold sizes.

    Try it on paper first.

Example 4: Spot the leakage

conceptual

An analyst standardises all 1,000 rows, selects the 20 features most correlated with the label using all rows, and then runs 5-fold CV on a classifier. The CV accuracy is 95% but the model does poorly on new data. Why?

  1. Step 1: Find the steps that used labels or statistics from all rows.

    Try it on paper first.

Previous year questions

1 official GATE DA question on this topic, newest first.

Practise these 1 PYQs →

Topic test

9 questions: up to 1 official PYQs and original practice questions, with GATE marking (MCQ wrong answers lose marks). Score 60% or more to pass. A new set is drawn each time.

Mark as mastered

Loading your progress…

My revision notes

Notes written by the GATEiit team against the official GATE DA syllabus; being checked by a reviewer. Updated 2026-11-21. Spotted a mistake? Use Report an issue on any question, or contact us.