Evaluation Metrics for Machine Learning Models
If you find yourself adjusting a model because of what the test score showed, the score you would report is no longer a measure of future performance.
The Question Behind Every Model Comparison
Machine learning practitioners spend much of their time trying different models, different settings, and different parameter values. At the end of that work they still need to answer one question: how well will the chosen model perform on new data it has never seen? Two tools exist to answer that question, cross-validation and a held-out test set. They are the everyday instruments for evaluating and improving learning algorithms, and they are easy to misuse.
The central point of this article is that the way the data is divided determines whether the score you report can be trusted. Two parts of the same dataset can look identical in a spreadsheet, yet one part can be used to make decisions and the other must stay out of them. Keeping that distinction clear is what makes an evaluation honest.
What do you think happens?
You have one dataset. You try several models on it, keep the one with the best score, and report that best score as your estimate of future performance. Is that estimate fair?
Reveal answer
No. The estimate is optimistic because the selection step used the same data that produced the reported score.
Choosing the best of several candidates is itself a search over the data. The winner partly reflects chance patterns in that particular dataset, so measuring the winner on the same data overstates how it will behave on new data.
What Happens When the Test Set Gets Used Up
The source makes two claims that work together. First, cross-validation, or the use of a test set, is what allows us to estimate how a machine learning model will perform in the future. Second, if the test set or cross-validation is also used to choose a model or to choose model parameters, the test data gets used up. Once that happens, the same data can no longer give a fair picture of future performance.
The reason is that a selection step is itself a search. Each time you compare candidates on a dataset and keep the winner, you let that dataset influence the final choice. The more candidates you compare, the more likely it is that one of them looks good on that particular data for reasons that will not carry over to new data. If you then measure the winner on the same data, you are measuring a score that was partly shaped by the choice itself. This is why the source describes the resulting estimates as overly optimistic.
A Walkthrough of the Test-Set Mistake
Comparing three candidate models the wrong way
A team has one labeled dataset and three candidate models. They want to pick the best model and report how well it will perform on new data. They first split off a test set, then compare all three candidates on that test set.
- 1Compare every candidate on the test set
Each model is scored using the test data, and the scores are recorded side by side.
- 2Keep the highest scorer
The team selects the model with the best test score. The test data has now influenced which model is kept.
- 3Report the winning test score
The team reports that same test score as the estimate of future performance. That number was computed on data that already shaped the choice.
- 4Identify the problem
The reported number is overly optimistic. The test set no longer represents unseen data for this decision, because it was used to make the decision.
The estimate looks impressive but cannot be trusted as a measure of future performance. The only change needed is to keep the test set out of every comparison until one final evaluation.
Three Roles for Three Parts of the Data
To avoid using up the test data, the source recommends splitting the available data into three parts, each with a separate purpose. The training data is used for model building: the model learns its patterns from it. The validation data is used for model and parameter selection: you compare candidate models and settings on it. The test data is used for model evaluation: you measure the final chosen model once, to estimate how it will perform in the future. Keeping these roles separate is what protects the final estimate.
Read this as a division of labor rather than a sequence of copies. The three parts do not overlap, and each one answers a different question. The training part answers what the model should learn. The validation part answers which model or parameter setting to keep. The test part answers how good the kept model really is. Because the test part was never consulted while making choices, its score is not contaminated by those choices.
Cross-Validation as a Replacement for a Simple Split
The three-way split is the simple version. The source points out that each of these splits can be replaced with cross-validation. Cross-validation is therefore not a competitor to the three-set idea. It is a way of carrying out the same roles with a more thorough use of the data, because every part of the data takes a turn serving in the evaluation role rather than being set aside once for a single split.
In the diagram, the data is divided into four slices. In each round, one slice is held out for validation and the others are used for training. The held-out slice changes from round to round, so every slice is used for validation exactly once. The validation results from the rounds are then used to compare candidate models and settings, which is the selection role described above.
Where Cross-Validation Fits in the Full Workflow
The most commonly used arrangement, according to the source, has two layers. One split divides the data into a training set and a test set, and the test set is used for model evaluation. Within the training set, cross-validation is used for model and parameter selection. The outer split protects the final estimate, and the inner cross-validation handles the choices. The test set stays untouched until the very end, while the training set is reused, through cross-validation, to decide which model and settings to keep.
- 1Split off the test set and set it aside without looking at its scores
- 2Run cross-validation on the remaining training set to compare candidate models and settings
- 3Select the model and parameter values that perform best in cross-validation
- 4Train the selected model on the training set
- 5Evaluate the chosen model once on the held-out test set and report that score
Mistakes Beginners Make with Evaluation Splits
Common mistake Choosing the best model by its test score
What it looks likeComparing five candidate models on the test set and reporting the highest test score as the final estimate.
Why it failsThe test data influenced which model was kept, so the reported score is partly a product of that choice and is overly optimistic.
Do this insteadUse the validation data or cross-validation on the training set to make the choice, and touch the test set only once at the end.
Common mistake Treating a single score on the data you trained on as a fair estimate
What it looks likeReporting the score a model earns on the same data it learned from as its expected performance on new data.
Why it failsThe question the evaluation must answer is how the model performs on data it has never seen. Data used for building or selecting the model does not answer that question.
Do this insteadKeep a test set that plays no role in building or choosing the model, and report the score from that set.
Common mistake Looking at the test set during development to check progress
What it looks likeRe-running the test evaluation after each tweak to see whether the score went up.
Why it failsEach look is a decision informed by the test data, which gradually uses it up.
Do this insteadTrack development progress with validation data or cross-validation, and reserve one final test evaluation.
Try It: Spot the Leak
A student splits a dataset into a training set and a test set. They train three models on the training set, compare their accuracy on the test set, keep the model with the highest test accuracy, and report that accuracy as the expected performance on new data. Identify which step uses the test data for a decision, explain why the reported number is overly optimistic, and describe how the workflow should be changed.
Hints
- Ask which data was consulted before the final evaluation and what decision it influenced.
- Think about which part of the data should be reserved for selecting models, and which part should be touched only once.
- The corrected workflow uses cross-validation on the training set to choose, and the test set to evaluate the final choice once.
Summary of the Evaluation Workflow
- 1A test set or cross-validation estimates future performance only if the test data is not used to make decisions.
- 2Using the test set to choose a model or parameter uses it up and produces overly optimistic estimates.
- 3A proper workflow separates training data for building, validation data for selection, and test data for one final evaluation.
- 4Cross-validation can replace a simple split, and the common pattern uses a training/test split with cross-validation on the training set for selection.
At a glance
- A test set or cross-validation estimates future performance only if the test data is not used to make decisions.
- Reusing the test set to choose among models or parameters makes the reported score overly optimistic.
- A proper workflow separates training data for model building, validation data for selection, and test data for one final evaluation.
- Cross-validation can replace a simple split, typically as a training/test split with cross-validation on the training set for selection.