Lesson 11: Validation: Train–Test Splits and Cross-Validation
In Lesson 3 you split your data into a training set and a test set, and that single split already protects you from the most common self-deception in machine learning: believing a model that has only memorised its homework. But a single split has a weakness of its own. If you happen to get a lucky split — an easy test set — your score looks better than the model deserves. If you get an unlucky one, it looks worse. Cross-validation is the standard answer: instead of trusting one split, you repeat the process many times and average the results.
The most common form is k-fold cross-validation. You divide the data into k equal parts, called folds — five or ten are the usual choices. Then you train the model k times: each time, one fold is held out as the test set and the other folds are used for training. Every example gets to be in a test set exactly once, and at the end you average the k scores. The average is a much steadier estimate than any single split, and the spread of the scores tells you something extra: if the scores jump around a lot from fold to fold, the model's performance is unstable, and you should be cautious about trusting it.
What cross-validation can and cannot do
Cross-validation is for estimating how well a model generalises and for comparing models fairly. If model A beats model B on all five folds, that is real evidence; if it wins on one fold and loses on the rest, it probably is not better at all. But cross-validation does not fix a bad model, and it does not create data. If your data is small or unrepresentative, averaging five unreliable scores gives you a reliably unreliable answer. It also costs time: training a model five or ten times takes five or ten times as long, which matters for large models.
There is one mistake that quietly ruins cross-validation, and it is worth learning early: data leakage. Leakage happens when information from the test fold sneaks into training. A classic example is scaling the features — adjusting them so they all have similar ranges — using the whole dataset before splitting it. The scaling then carries information about the test examples into the training, and the scores look better than they should. The rule is simple: every step that learns something from the data must happen inside each fold, after the split, never before it. Get this right and your scores mean what they say; get it wrong and you will be surprised the first time the model meets the real world.
Keeping the classes balanced across folds
Cross-validation has a subtlety that catches beginners: if the data is uneven — say, one case in a hundred is positive — then a random split can easily produce a fold with no positive cases at all, and a model tested on that fold learns nothing about how it handles the rare class. The fix is stratified cross-validation, where each fold is built to contain the same proportion of each class as the whole dataset. With stratification, every fold is a fair miniature of the whole problem, and the averaged score means what it says. The same idea applies to the original train-test split: a test set that contains no examples of a rare but important class cannot tell you anything about how the model treats that class. Stratification is a small detail, but it is exactly the kind of detail that separates a score you can trust from one that quietly misleads you — and most modern tools apply it by default for classification, so it costs nothing to check that it is switched on.
A final point about honesty: cross-validation scores are still estimates, not guarantees. The data you have may not match the data the model will meet tomorrow — customers change, seasons change, a pandemic changes everything. The best validation in the world is a model running in the real world with its predictions checked against what actually happens. Cross-validation is the best laboratory test we have, and you should use it every time — but never confuse a laboratory test with the real thing.
Key Terms
- Cross-validation
- A method of testing a model by splitting the data into folds, training on all but one fold and testing on the held-out fold, repeated until every fold has been tested.
- Fold
- One of the equal parts the data is divided into for cross-validation.
- k-fold cross-validation
- Cross-validation with k folds, so the model is trained and tested k times and the scores are averaged.
- Data leakage
- When information from test data accidentally influences training, making the scores look better than the model deserves.
- Feature scaling
- Adjusting features so they share a similar range, which many models need to work well; it must be done separately inside each fold.
- Estimate
- An approximation of a model's true performance, based on the data available; every validation score is an estimate, not a guarantee.
Discussion Questions
- Why is averaging five test scores more trustworthy than a single test score?
- A student scales all the features using the whole dataset, then splits it for cross-validation. What has gone wrong, and why does it matter?
- Cross-validation gives a good laboratory score, but the real world still surprises models. What could change between the lab and the real world?
Practice Questions
- What is k-fold cross-validation, in one sentence?
- In 5-fold cross-validation, how many times is the model trained?
- Why does the spread of fold scores matter as well as the average?
- What is data leakage? Give an example.
- Why must feature scaling happen inside each fold, after the split?
Show answers
- Split the data into k folds, train and test k times with a different fold held out each time, and average the scores. Every example is tested exactly once, so the average is a steadier estimate than a single split.
- Five times. Each fold takes a turn as the test set, so there are five training runs in total.
- A wide spread means the model's performance is unstable. If scores jump around between folds, you cannot trust the average to hold on new data.
- Test information sneaking into training, such as scaling features using the whole dataset before splitting. The scaling carries information about the test examples into training, so the scores look better than they deserve.
- So that nothing learned from the test fold can influence the training. Any step that learns from data must only see the training part, or the test score stops being an honest check.