Part 2 · Lesson 11 · 6 min
Overfitting and underfitting
Why does a model that is perfect in the lab fall apart in the real world?
Spoiler: perfect was the warning sign.
Think back to school. Some students learn the subject. Others memorize last year's answer sheet. On a repeat exam, the memorizer wins. On a new exam, the memorizer is in trouble.
The memorizer
Models can do the same thing. A model that memorizes its training examples, including their random quirks, is overfitting. It looks brilliant on data it has seen and stumbles on data it has not.
The opposite problem is underfitting: the model is too simple to catch the real pattern, so it is mediocre everywhere. The fix for both starts with the same habit from the last lesson: score the model on data it has never seen.
Two scores tell you which problem you have
So the two scores are a quick diagnosis. Low on both means too simple. High on practice and low on new data means memorizing. The goal is a model that is good on both, with a small gap between them.
The lab and the world
Overfitting is not a rare bug. It is one of the most common reasons a model disappoints after launch. And a bigger, more complex model makes it easier to memorize, unless you also give it more, and more varied, data.
Go deeper optional
Picture a scatter of dots that follow a gentle curve. A flat line through them is too simple (underfitting). A wild zig-zag that touches every dot is too wiggly (overfitting). A smooth curve through the middle is just right, and it is the only one that predicts the next dot well.
Engineers watch this on a generalization curve: training error and validation error plotted as training goes on. Training error keeps falling. Validation error falls, then starts rising again. That turning point is where memorizing begins.
Common remedies: more and more varied data, a simpler model, and early stopping, which means stopping training near that turning point.
Check yourself
1. A model scores 99% on its training data and 60% on new data. What is likely happening?
A big gap between training and new data means it memorized. Underfitting would score poorly even on the training data.
2. A model scores 55% on both training and test data. What is likely happening?
Low scores on both mean the model is too simple. Overfitting shows a big gap, not low scores everywhere.
3. Which usually helps reduce overfitting?
Varied examples push the model to learn the real pattern. Testing on training data only hides the problem.