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

Free lesson

Testing on data the model has not seen

After this lesson: I can explain why we test on data the model has not seen.

40 min lessonNo commands

Perfect on Friday, wrong on Monday

An IT team builds a model to spot urgent support tickets. They check it on the same 500 tickets it learned from. It gets 499 right.

On Monday it goes live. It misses a server that is down and flags a request for a new chair as urgent.

The check on Friday told them nothing useful. This lesson is about why.

By the end of this lesson

Note

I can explain why we test on data the model has not seen.

Key words

WordWhat it meansExample
Training setthe examples a model learns from
Test setexamples kept back to check the model
Unseen dataexamples the model never learned from
Splitdividing the examples into two sets80% and 20%
Leakagetest examples getting into training

The cells students fill in are left blank in this preview.

A model must work on new cases

A model is only useful on cases it has not met yet: tomorrow’s parcels, Monday’s tickets.

Checking it on the examples it learned from is like checking a new colleague with the exact cases from their training. They may have remembered the answers.

So you keep some examples back. The model never sees them while it learns. They show how it will do on unseen data.

The same model, checked two ways

Checked onRightWhat it tells you
The 500 training tickets499 of 500Very little: it has seen them
100 tickets kept back62 of 100Roughly how it will do on Monday

The cells students fill in are left blank in this preview.

Split the examples before you train

Before training, you split the examples into two sets.

The training set is the bigger part, often 80%. The model learns from it.

The test set is the rest, often 20%. You use it once, at the end, to measure the model.

With 1,000 tickets, that is 800 to learn from and 200 to test on.

Working out a split

ExamplesTraining shareTraining setTest set
1,00080%800200
50080%400100
1070%73

The cells students fill in are left blank in this preview.

Split some tickets

Code: python
# 10 past support tickets, oldest first.
tickets = ["printer", "password", "server down", "new mouse", "data lost",
           "email slow", "site offline", "new chair", "VPN broken", "font size"]

percent = 80
cut = len(tickets) * percent // 100

training = tickets[:cut]
testing = tickets[cut:]

print("Train on", len(training), "tickets:", training)
print("Test on", len(testing), "tickets:", testing)

# Change percent to 70, then press Run.
# How many tickets does the test set get now?

Leakage spoils the test

The test only works if the test examples stay out of training. When one gets in, that is leakage.

Leakage is easy to cause by accident. You might copy a ticket twice, or train again on all the data after testing.

A model with leakage looks better than it is. A hiring manager will expect you to check for it.

A common mistake

Warning

Many people think a high score on the training data proves a model works. It proves only that the model fits what it has seen. Trust a score only when it comes from a test set the model never learned from.

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