Lesson 2.6 · 8 min read · about 55 min to master at a very slow pace
Training, Validation and Test Sets
What each data split is for, typical split sizes, and why repeatedly checking the test set and picking the best model quietly ruins your performance estimate.
Three sets, three jobs
| Set | Used for | Rule |
|---|---|---|
| Training | Fitting the parameters (e.g. ) | Never the sole evidence of quality |
| Validation | Comparing models, tuning hyperparameters (degree, , , threshold) | Can be reused, but each reuse slightly overfits it |
| Test | One final, honest estimate of performance on new data | Touch it once, after all decisions are made |
With and a 60:20:20 split you get 600 training, 200 validation and 200 test observations. After choosing, the final model is often re-trained on training + validation data.
The single most important rule of ML evaluation
You only get an unbiased estimate of performance by evaluating on data that played no role whatsoever in fitting or choosing the model.
Why picking the best test score is a leak
Scenario: 40 models
A bank has 10,000 records: 7,000 training, 1,500 validation, 1,500 test. A data scientist trains 40 models and checks each one's test accuracy: 85%, 87%, 89%, ..., up to 96% for model 40. They select model 40 and report 96%.
No model was trained on test rows, so where's the problem? The choice used the test set. Among 40 candidates, the winner partly won by luck on those particular 1,500 rows. That's selection bias: the reported 96% is too optimistic.
Correct: train on the training set, compare on the validation set (or by cross-validation), select the best, then evaluate that one model on the test set once.
Stratified splitting
When one class is rare (say 2% fraud), a random split can leave very few positives in a small validation or test set. A stratified split keeps the class proportions the same in every subset.
Stuck? Common doubts
Questions real learners asked while studying this lesson. They stay closed so they don't spoil your own thinking: open only the ones you need.
Going deeperWho actually "learns" the test set: the data scientist or the model?
The data scientist, or any automated selection procedure. Each of the 40 models is trained from scratch on the training data; none of them sees test rows or remembers earlier models.
But the person looks at test results and decides what to try next and which model to keep. Through those decisions, the overall development process adapts to the test set. Leakage happens through human (or automated) model-selection decisions, not through the training of any single model.
Going deeperWhy is choosing model 40 because it has the highest test accuracy a problem?
Repeatedly checking test results influences every later development decision, so the selection process indirectly fits the test set. With 40 tries, some models score well on that particular test set by chance. Picking the maximum keeps that luck, so the final reported test accuracy is overly optimistic: expect less on genuinely new data.
Cheatsheet
Train / validation / test
Typical splits
Check your understanding
Score 75% or more to master this lesson and earn XP. Answer first, then check.