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
§ 6.1.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 / Aspect | Hold-out | k-fold (k = 5 or 10) | Leave-one-out (LOOCV) |
|---|---|---|---|
| Model fits | 1 | k | n |
| Training set size | Often 50–80% of n | n(k − 1)/k | n − 1 |
| Bias of the error estimate | Highest: trains on the least data | Moderate | Lowest: trains on almost all data |
| Variance of the estimate | High: depends on one random split | Moderate | Higher than k-fold: the n fits are almost identical, so their errors are highly correlated |
| Randomness | Changes with the split | Changes with the fold assignment | None: 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.
3. Worked example: 5-fold CV by hand
Five equal folds of 100 points.
↳ Result: 20 points per foldEach fit trains on the other four folds.
↳ Result: 80 points per fit, 5 fits in totalEqual fold sizes, so a simple mean works.
4.The LOOCV shortcut for least squares
§ 6.4.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
§ 6.5.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.
Practice
How many times is the model trained in 10-fold cross-validation on 500 data points?
With n = 200 and 5-fold CV, how many points is each model trained on?
Compared with 10-fold CV, the LOOCV estimate of test error typically has:
You standardise all features using the mean and standard deviation of the full dataset, then run 5-fold CV. What is wrong?
One-minute revision
“Train on part, test on the rest, repeat, average. k-fold = k fits; LOOCV = n fits.”
- •CV₍ₖ₎ = (1/k) Σ MSEᵢ
- •Train size in k-fold = n(k − 1)/k
- •LOOCV (least squares) = (1/n) Σ ((yᵢ − ŷᵢ)/(1 − hᵢ))²
- •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
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.