What learning is · lesson 5 of 13
Parts in this lesson
Atrain_test_split(X, y, test_size=0.25, stratify=y)a training set and a test set
B(predicted == y_test).mean()accuracy, by hand
Cmodel.score(X_test, y_test)accuracy, from the model

Training and testing

Last updated

A model is only useful if it works on data it hasn’t seen. So before training, you set some data aside, train on the rest, and score the model on what you kept back. This lesson shows how to split data properly with train_test_split, how to score a model, and what happens if you skip the split: a perfect score that means nothing.

By the end of this lesson you can

  • Explain why a model must be scored on data it didn’t learn from
  • Split data into training and test sets with train_test_split, keeping the class mix
  • Compute accuracy, by hand and with a model’s score method
  • Compare training and test scores, and say what a gap between them means

Why a model can’t mark its own homework

A student who has seen the exam questions in advance can score 100% without understanding anything. A model is the same: scored on the examples it learned from, it can look perfect just by remembering them. k-NN with k=1 does exactly that, as step 5 shows.

What you want to know is how the model will do on new data, the penguins it'll be asked about next year. You don't have next year's penguins, so you pretend: before training, deal some of your labelled data into a test set and lock it away. Train on the rest, the training set. Then score the trained model, once, on the test set. That score estimates how the model generalises: how well it does beyond the data it learned from.

Split shuffle20 penguins
Training pile: the model learns from these
  • 15
  • 16
  • 4
  • 2
  • 12
  • 9
  • 19
  • 20
  • 10
  • 17
  • 3
  • 14
  • 18
  • 7
  • 11
15 penguins: 5 Adelie, 3 Chinstrap, 7 Gentoo
Test pile: kept aside until the end
  • 8
  • 6
  • 13
  • 5
  • 1
5 penguins: 4 Adelie, 1 Chinstrap, 0 Gentoo

Test pile: 4 Adelie, 1 Chinstrap, 0 Gentoo. Deal a few times: sometimes a species is missing from the test pile, or nearly all of it lands there.

Shuffle before dealing: data files are often sorted (the penguins file lists all the Adelies first), and a split that took the last quarter would test on Gentoos alone.

train_test_split

scikit-learn's train_test_split shuffles and deals in one call:

split.ipynb
  • test_size=0.25 keeps a quarter for testing: 86 of the 342 penguins. A quarter to a fifth is usual; with very large datasets, a smaller share is plenty.
  • random_state=0 fixes the shuffle, so you (and this page) get the same split every run. Without it you'd get a different split, and slightly different scores, each time.
  • stratify=y deals each species separately, so both sets keep the same mix (113, 51 and 92 against 38, 17 and 31: about 44%, 20% and 36% in each). Without it, a small test set can end up short of a rare class by chance. Try it in the figure: deal a few times without "Keep the species mix".

It returns four arrays, always in this order: X_train, X_test, y_train, y_test.

Accuracy

The simplest score for a classifier is accuracy: the share of test examples it labels right.

accuracy=1n∑i=1n𝟙[yˆi=yi]

In words: for each of the n test examples, count 1 if the prediction yˆi matches the true label yi and 0 if not, then take the mean. (𝟙[…] means "1 if this is true, else 0".) In NumPy: (predicted == y_test).mean(), the mean of a Boolean array, as in lesson 1.9. Every scikit-learn classifier also has a score method that does the same:

score.ipynb

98.8%: 85 of the 86 test penguins. Notice the scaling: the means and standard deviations come from the training set only, and the same numbers are then used to scale the test set. The test set is supposed to be unseen, and that includes not helping to choose the scaling. Lesson 2.10 explains why this matters and how to make it automatic.

Training score against test score

Comparing the score on the training set with the score on the test set tells you a lot:

score.ipynb

Here they're nearly the same, 98.4% and 98.8%: the model does as well on new penguins as on the ones it learned from. (The test score is a shade higher only by luck: 86 penguins is a small sample, and one penguin is worth 1.2 percentage points.)

The pattern to watch for is a big gap: high training score, much lower test score. It means the model learned things true of its training examples but not of penguins in general: noise, quirks, individual birds. That's overfitting, and it's the subject of the next lesson.

Wrong way: testing on the training set

Here's a harder problem than the penguins: 300 points from two classes that overlap a lot (scikit-learn's make_moons with plenty of noise). Train k-NN with k=1, and score it both ways:

homework.ipynb

On its own training data it's perfect, 1.0: each training point's nearest neighbour is itself, so it always "predicts" its own label. On the test set it gets 82.2%. If you had only scored it on the training data, you'd have reported a perfect model and shipped one that's wrong about 1 time in 6.

This mistake is easy to make by accident: score the model on X instead of X_test, or forget to split at all. Whenever a score looks too good, check which data it was computed on.

Check what you learned

train_test_split(X, y, test_size=0.2) on 500 examples. How many are in the test set?
Predict, then check in step 5’s notebook: what does k-NN with k=1 score on its own training data?
A model scores 99% on its training set and 70% on its test set. What’s the most likely explanation?

Sources

Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.

All sources for Simple Machine Learning