Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Spotting a model that learned by heart

After this lesson: I can spot a model that learned the examples by heart.

40 min lessonNo commands

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

WordWhat it meansExample
Overfittinglearning the examples by heart, not the pattern
Underfittingnot learning even the examples well
Training scorehow well a model does on its training set
Test scorehow well a model does on its test set
Gapthe training score minus the test score95 - 70 = 25
Noisechance 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

Code: python
# 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 scoreTest scoreGapWhat it means
100%55%45Overfitting
92%89%3Good
61%59%2Underfitting
97%72%25Overfitting

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.

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.