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. -NN with 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.
- 15
- 16
- 4
- 2
- 12
- 9
- 19
- 20
- 10
- 17
- 3
- 14
- 18
- 7
- 11
- 8
- 6
- 13
- 5
- 1
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:
test_size=0.25keeps 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=0fixes 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=ydeals 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.
In words: for each of the test examples, count 1 if the prediction matches the true label 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:
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:
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 -NN with , and score it both ways:
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
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.
- Goodfellow, Bengio and Courville, Deep Learning, chapter 5: Machine Learning Basics: training error, test error and generalisation (5.2)
- scikit-learn: train_test_split: test_size, random_state and stratify
- scikit-learn: Cross-validation: evaluating estimator performance: why testing on the training data is a methodological mistake