Training, Testing and Overfitting
Learn why models are tested on data they have never seen, how overfitting makes a model memorize instead of learn, and how to tell if a model generalizes.
Welcome back!
Step 1 of 11Learn
The student who memorized the answers
Two students prepare for a math test using last year's practice exam. Liam memorizes every answer: question 1 is 42, question 2 is 17. Ava works through each problem until she understands the method. On the practice exam, both score 100%. On the real test, with new numbers, Liam fails and Ava aces it.
Machine learning models face exactly the same risk. A model can perform brilliantly on its training data simply by memorizing it, and then fall apart on anything new. What we actually want is a model that generalizes: one that has learned the underlying pattern and works on examples it has never seen.
That's why data scientists split their data before training:
- The training set (often around 70 to 80% of the data) is used to learn the parameters.
- The test set (the rest) is locked away and used only at the end to measure performance on unseen examples.
The test score is the honest one. Reporting how well a model does on its own training data is like Liam bragging about his practice exam.
A golden rule follows: test data must never leak into training. If even some test examples were used for training, the test score will look better than the model really is.
Step 2 of 11Compare
Memorizing versus generalizing
| Aspect | Liam (memorizes) | Ava (generalizes) |
|---|---|---|
| How he or she studies | Memorizes each answer | Learns the method |
| Practice exam | 100% | 100% |
| Real test, new numbers | Fails | Aces it |
| In machine learning | Overfitting: great on training data, poor on new data | Generalization: works on data it has never seen |
Step 3 of 11Practice · True or false
A model that scores 99% on its training data is guaranteed to work well on new data.
Step 4 of 11Learn
Overfitting and underfitting
Overfitting is the technical name for Liam's problem: the model learns the training data too closely, including random noise and quirks that don't apply in general. A classic sign is a big gap between a very high training score and a much lower test score.
Imagine predicting a person's height from their age using data from 20 kids. A sensible model draws a smooth curve: kids grow taller with age. An overfitted model zigzags through every single data point, including one unusually tall 9-year-old, and predicts that all 9-year-olds are giants.
The opposite problem is underfitting: the model is too simple to capture the real pattern, like predicting the same height for every age. It does badly on both training and test data.
Ways to reduce overfitting include:
- More, and more varied, training data, so quirks average out.
- Simpler models with fewer adjustable parameters when data is limited.
- Stopping training early, before the model starts memorizing.
- Checking performance on held-out data during development (often called a validation set) to catch the gap as it opens.
The goal is the sweet spot between the two: complex enough to capture the real pattern, simple enough to ignore noise.
Step 5 of 11Explore
Finding the sweet spot of complexity (illustrative)
The curve shows test error as model complexity grows. Simple models underfit, so error is high on the left. Very complex models memorize noise, so test error climbs again on the right. Move the complexity slider and compare training error with test error in the readouts. The formula is a teaching sketch, not data from a real model.
- Training error
- 15 %
- Test error
- 17 %
- Best complexity for this noise
- 3.1
Step 6 of 11Practice · Sort into groups
Is each model overfitting, underfitting or generalizing well?
Tap an item, then tap its group (or drag it there).
Step 7 of 11Learn
Garbage in, garbage out
Even a perfectly tuned model can't beat the data it learns from. Data scientists often say garbage in, garbage out. Problems to watch for:
- Too little data: a handful of examples can't capture real variety.
- Unrepresentative data: a voice assistant trained mostly on adult voices may struggle with children's voices. A skin condition detector trained mostly on light skin may miss conditions on darker skin.
- Wrong labels: if some training photos of cats are labeled "dog," the model learns confusion.
- Shortcuts: models sometimes find clues that work in the training data but have nothing to do with the real task. In one well-known kind of case, a model learned to identify certain medical images partly from labels or rulers that happened to appear in them, rather than from the medical features themselves.
This is why careful teams spend more time collecting, cleaning and checking data than on the algorithm itself. They ask: Who and what is represented? Who is missing? Are the labels correct? Does the data look like the real world where the model will be used?
When you hear about an AI system, asking "What was it trained on?" is one of the most useful questions you can ask.
Step 8 of 11Flashcards
Garbage in, garbage out
Tap a card to flip it.
Step 9 of 11Note
Step 10 of 11Practice · What would you do?
What would you do?
Maya builds a model that predicts whether a plant is healthy from leaf photos. It scores 98% on the photos she trained it with, but only 61% on new photos her friend takes. What should she conclude first?
Step 11 of 11Practice · Choose one
Why must test data be kept completely separate from training data?
Lesson complete!