MACHINE LEARNING/Student Engineering Notebook
Page 15·notes
CLUSTERING, PCA & MODEL EVALUATION METRICS—~26 MIN READ

Cross-Validation: Hold-Out, k-Fold and Leave-One-Out

GATE 2027 DA core: cross-validation methods such as leave-one-out (LOO) and k-fold cross-validation

“How to estimate test error honestly when you only have one dataset, and how leakage quietly ruins it.”

1.Why training error lies

Prerequisites: bias-variance trade-off and evaluation metrics from [model evaluation, ROC-AUC and bias-variance](/notes/machine-learning/model-evaluation-roc-auc-and-bias-variance).

A model is fitted to make its training error small, so training error is an optimistic estimate of how it will do on new data. A flexible enough model can drive training error to zero and still predict badly.

We want the test error, but we usually have one dataset and no separate test set. Cross-validation simulates a test set by holding some data out of training, measuring error on it, and repeating.

2. Three ways to hold data out

Feature / AspectHold-outk-fold (k = 5 or 10)Leave-one-out (LOOCV)
Model fits1kn
Training set sizeOften 50–80% of nn(k − 1)/kn − 1
Bias of the error estimateHighest: trains on the least dataModerateLowest: trains on almost all data
Variance of the estimateHigh: depends on one random splitModerateHigher than k-fold: the n fits are almost identical, so their errors are highly correlated
RandomnessChanges with the splitChanges with the fold assignmentNone: there is only one way to leave one out

✎ Takeaway: 5- or 10-fold CV is the usual default: close to LOOCV's low bias, with lower variance and far fewer fits.

WORKED NUMERICAL · STUDENT DERIVATION✎ Key Takeaway: LOOCV on the same data would need 100 fits, each on 99 points.

3. Worked example: 5-fold CV by hand

Problem Statement:
A dataset has n = 100 points. You run 5-fold cross-validation on a regression model, and the five held-out folds give mean squared errors 4.0, 5.0, 3.0, 4.5 and 3.5. How many points does each fit train on, and what is the CV estimate?
Step 1: Fold size

Five equal folds of 100 points.

↳ Result: 20 points per fold
Step 2: Training size

Each fit trains on the other four folds.

↳ Result: 80 points per fit, 5 fits in total
Step 3: Average the fold errors

Equal fold sizes, so a simple mean works.

CV₍₅₎ = (4.0 + 5.0 + 3.0 + 4.5 + 3.5) / 5
↳ Result: CV₍₅₎ = 4.0
Final Answer:Each fit trains on 80 points, and the 5-fold CV estimate of test MSE is 4.0.

4.The LOOCV shortcut for least squares

For linear regression fitted by least squares, you do not need n separate fits. With ŷᵢ the fitted value from the full model and hᵢ the leverage (the i-th diagonal entry of the hat matrix H = X(XᵀX)⁻¹Xᵀ):

CV₍ₙ₎ = (1/n) Σ ((yᵢ − ŷᵢ) / (1 − hᵢ))²

Each ordinary residual is inflated by 1/(1 − hᵢ). High-leverage points pull the fit towards themselves, so their full-data residual understates how wrong the model would be without them. The hat matrix is symmetric and idempotent, which is covered in [partitioned and idempotent matrices](/notes/linear-algebra/partitioned-and-idempotent-matrices).

5.Using CV well: model selection and leakage

CV is mostly used to choose between models or settings: k in k-NN, λ in ridge regression, tree depth. Compute the CV error for each candidate, pick the lowest, then refit that choice on all the data.

Leakage is the classic trap. If you standardise features, fill missing values or select features using the whole dataset before splitting, information from the validation fold has already shaped the training. The CV error then looks better than reality. Every data-dependent step must be fitted inside each training fold, then applied to that fold's validation part.

For classification with rare classes, use stratified k-fold so every fold has a similar class balance. For time-ordered data, train on the past and validate on the future instead of shuffling.

STUDENT PRACTICE · CONCEPT DRILL

Practice

Problem 1 · FOUNDATION

How many times is the model trained in 10-fold cross-validation on 500 data points?

Problem 2 · FOUNDATION

With n = 200 and 5-fold CV, how many points is each model trained on?

Problem 3 · GATE-LEVEL

Compared with 10-fold CV, the LOOCV estimate of test error typically has:

Problem 4 · TRICKY

You standardise all features using the mean and standard deviation of the full dataset, then run 5-fold CV. What is wrong?

RAPID REVIEW · NOTEBOOK SUMMARY

One-minute revision

core principle:

“Train on part, test on the rest, repeat, average. k-fold = k fits; LOOCV = n fits.”

must-remember principles:
  • •CV₍ₖ₎ = (1/k) Σ MSEᵢ
  • •Train size in k-fold = n(k − 1)/k
  • •LOOCV (least squares) = (1/n) Σ ((yᵢ − ŷᵢ)/(1 − hᵢ))²
critical exam pitfalls:
  • •LOOCV: low bias, high variance
  • •No preprocessing on the full data before splitting
  • •Refit the chosen model on all data
  • •Stratify classes; don't shuffle time series
SOURCE TRANSPARENCY & CITATIONS

Primary Academic References & Exam Sources

Every concept in Notes From a B.Tech Brain is synthesized from authoritative academic texts, seminal peer-reviewed research, verified exam archives, and renowned university lectures.

Primary Academic Texts
An Introduction to Statistical Learning↗by · Chapter 5 (Resampling Methods): validation set, LOOCV and its least-squares shortcut, k-fold, bias-variance of CV.
ACADEMIC STANDARD
The Elements of Statistical Learningby · Section 7.10: cross-validation, including the wrong and right way to do it with feature selection.
ACADEMIC STANDARD
Official Examination Archives
GATE 2027 DA syllabus↗IIT Madras · Machine Learning: cross-validation methods such as LOO and k-fold.
GATE CS OFFICIAL