100% on Friday, 55% on Monday
An estate agent’s team trains a model to guess house prices. On the houses it learned from, it is never wrong.
On new houses it is wrong about half the time. One flat is priced at twice its worth.
The model did not learn what makes a house cost more. It learned the answers by heart.
By the end of this lesson
Note
I can spot a model that learned the examples by heart.
Key words
| Word | What it means | Example |
|---|---|---|
| Overfitting | learning the examples by heart, not the pattern | |
| Underfitting | not learning even the examples well | |
| Training score | how well a model does on its training set | |
| Test score | how well a model does on its test set | |
| Gap | the training score minus the test score | 95 - 70 = 25 |
| Noise | chance details that are not the pattern |
The cells students fill in are left blank in this preview.
Overfitting is remembering instead of learning
A good model learns the pattern: bigger houses near the station cost more.
An overfitting model learns every detail of its examples, even the noise. Number 12 sold for more because the buyer was in a hurry. That is chance, not a pattern.
The model then does very well on the examples and badly on anything new.
A model that only remembers
# A "model" that only remembers: it looks each house up.
training = {"12 Mill Lane": 210000, "4 High Street": 185000, "9 Park Road": 320000}
def predict_price(address):
if address in training:
return training[address]
return 0 # It has never seen this house, so it has no idea.
print("Training house:", predict_price("4 High Street"))
print("Training house:", predict_price("9 Park Road"))
print("New house:", predict_price("7 Station Road"))
# Add "7 Station Road" with a price to training, then press Run.
# Did the model get better at houses it has never seen?
The gap between two scores gives it away
You spot overfitting by comparing two scores from the last two lessons.
Measure the model on the training set, then on the test set. Subtract the test score from the training score. That is the gap.
A small gap is normal. A big gap means the model remembered its examples. If both scores are low, the model is underfitting: it has not learned enough.
Reading the two scores
| Training score | Test score | Gap | What it means |
|---|---|---|---|
| 100% | 55% | 45 | Overfitting |
| 92% | 89% | 3 | Good |
| 61% | 59% | 2 | Underfitting |
| 97% | 72% | 25 | Overfitting |
The cells students fill in are left blank in this preview.
A common mistake
Warning
Many people think 100% on the training data means a perfect model. Usually it means the opposite. A perfect training score with a much lower test score is the clearest sign of overfitting.
What a team does about it
For overfitting, give the model more examples, so chance details matter less. Or use a simpler model that cannot remember so much. Or stop training sooner.
For underfitting, do the opposite. Give the model more useful features, or a model that can learn more.
Then measure both scores again.