Cross-Validation
Estimating model generalisation by repeatedly training on subsets and evaluating on the held-out remainder â k-fold, leave-one-out.
Cross-validation is a technique for estimating how well a model will perform on unseen data, by testing it on different subsets of the available data.
The most common version is k-fold cross-validation:
- Split data into equal folds
- For each fold: train on the other folds, evaluate on this fold
- Report the average evaluation score across all folds
Common values: or . Leave-one-out (LOO): .
Cross-validation answers: "If I train on most of my data and test on the rest, how accurate am I â reliably, not just by luck?"
- Every data point is used for both training and validation exactly once across all folds
- Averaging across folds reduces the variance of the performance estimate compared to a single train/test split
- The choice of trades estimator bias (smaller , less training data per fold) against variance and compute cost (larger )
- Results in independently trained models, not one â the final deployed model is typically retrained on all the data
- Letting any information from the validation fold leak into training: feature scaling, imputation, or feature selection must be fit only on the training folds, never on the full dataset before splitting
- Using ordinary k-fold CV on time series: random folds let the model "see the future" â always use time-respecting splits (walk-forward validation) for temporal data
- Tuning hyperparameters using the final test set: this silently turns the test set into part of training, biasing the reported performance optimistically
You have 100 training examples. You try for a k-NN classifier, evaluating each with 5-fold CV.
| CV accuracy | |
|---|---|
| 1 | 68% |
| 3 | 78% |
| 5 | 82% |
| 7 | 80% |
| 9 | 76% |
Choose . Now train on all 100 examples and deploy.
Why shouldn't you use your test set to tune hyperparameters, even if you never use it during training?
Solution
If you tune hyperparameters based on test set performance, the test set becomes effectively a training set â your model is adapted to it. The test score no longer measures generalization to truly unseen data; it measures performance on "the test set specifically." This overfitting is subtle and hard to detect, but the resulting performance estimate is optimistically biased. Rule: the test set is touched exactly once, after all decisions are made.
Related concepts
Needs first