training and test data, and fitting too closely
learning from examples
what it is
A rule learnt from points must work on points it has never seen. So some points are held back. The rule is made from the training points and checked on the test points. A rule that fits its own points too closely does worse on new ones.
A machine learns a rule from points whose answers are known. Take twenty points on a plane, each a triangle or a square. Triangles belong in the middle of the plane and squares round its edge. Split the points in two. Ten are training points: the rule is made from them. Ten are test points: the rule never sees them while it is made. Two training points carry the wrong shape, a square at 6 across, 5 up and a triangle at 0 across, 3 up. Call them the odd points. Real points are often like that, and books call it noise.
Training points
Test points
The rule here is k-nearest neighbours: a new point takes the shape most of its k nearest training points hold. The rule gets a point wrong when it gives that point a different shape from the one it carries. This holds for the odd points too: the odd square counts as wrong if the rule calls it a triangle.
Start with k at 1. Checked on its own training points, the rule gets none wrong, since each point is its own nearest. It looks perfect. Checked on the test points, it gets 4 of 10 wrong. Three of the four stand nearest an odd point and copy its mistake. The fourth, a square at 7 across, 1 up, is as near the triangle at 5 across, 4 up as the square at 10 across, 3 up. As on the k-nearest neighbours page, the tie goes to the one listed first, the triangle.
The rule has learnt its own points by heart, noise and all. Books call that overfitting. Call the count of points wrong the error. Here the training error is 0 and the test error is 4. Only the test error says how the rule does on new points.
| k | training points wrong | test points wrong |
|---|---|---|
| 1 | 0 | 4 |
| 2 | 0 | 4 |
| 3 | 1 | 1 |
| 4 | 1 | 2 |
| 5 | 3 | 4 |
| 6 | 3 | 4 |
| 7 | 4 | 5 |
| 8 | 4 | 6 |
| 9 | 4 | 7 |
Now let more neighbours vote, so that no single point decides alone. At k = 3 the odd square at 6 across, 5 up is outvoted by the two triangles nearest it. So the rule now gets 1 of its own points wrong. The odd triangle at 0 across, 3 up keeps its shape, as its two nearest neighbours are triangles too. The test error falls from 4 to 1.
Let too many vote and nearness hardly counts. At k = 9, nine of the ten training points vote on every spot. The ten are five triangles and five squares. At each spot the one left out is the farthest training point. Its shape has 4 votes against 5 and loses at that spot. The rule gets 4 of its own points wrong and 7 test points. Books call a rule that does badly on both sets underfitting.
Training points
Test points
With k at 3, the rule gets 1 of its 10 training points wrong and 1 of the 10 test points.
With scripts on, a slider sets k. Both planes, the chart and this line change with it.
Follow the two lines on the chart. The training error starts at 0 and climbs as k grows. The test error falls, then climbs again. The test line is lowest at k = 3, so choose k there. Never choose by the training line: it is always 0 at k = 1, and no k can beat 0.
One caution. Here the test points chose k = 3, so they helped make the rule, and their 1 wrong is too kind. So split three ways: training points make the rule, validation points choose k, and test points, unseen until the end, give the last check.
where the formulas come from
Each question is worked out one numbered line at a time. A line shows the move it makes, the line itself, and why the move is allowed. A line either follows from the lines above it, or brings in something the question needs, such as how the method works, a small fact about , or a result from another page, with its own reason. Press 'hide the lines, then guess each one' to cover the lines and uncover them one by one, guessing each before you see it.
the move: take the test points away
the line:why it is allowed: A fifth of the points is divided by 5. The training points are the rest.
the move: write n in fifths
the line:points test points, a fifth training points 10 2 8 20 4 16 50 10 40 100 20 80 A fifth held back at four sizes. The test points and the training points always add back to all the points. why it is allowed: Five fifths of make , so both parts now stand over 5.
guess it, thenthe move: take one fifth from five
the line:why it is allowed: Five fifths take away one fifth leaves four fifths. For 50 points, that is 40 training points and 10 test points.
guess it, then
try it with a small : at , line 1 gives , which you can work out by hand
a worked example
Two students get 10 practice questions with answers, then sit 10 new questions. Ana learns the practice answers by heart. Ben learns the method. Who learnt more?
- On the practice questions Ana scores 10 and Ben scores 9. Judged there, Ana wins.
- On the new questions Ana scores 4 and Ben scores 8. Judged there, Ben wins.
- Ana drops by 6, from 10 to 4. Ben drops by 1, from 9 to 8. A big drop from practice to new questions is the sign of learning by heart.
| student | practice questions right | new questions right | drop |
|---|---|---|---|
| Ana | 10 | 4 | 6 |
| Ben | 9 | 8 | 1 |
the trap
check yourself
A rule gets 0 of 20 training points wrong and 9 of 20 test points. What does that say?
the answer
It has learnt its own points by heart, which is overfitting. Judge it by the test points, where it gets 9 of 20 wrong.
In the picture at k = 9 the rule gets 4 training points and 7 test points wrong. Is that overfitting?
the answer
No. An overfitted rule does well on its own points and badly on new ones. This rule does badly on both sets, which is underfitting.
You have 60 points and hold back a fifth as test points. How many of each are there?
the answer
A fifth of 60 is 12 test points. That leaves 48 training points, four fifths of 60, as the question above works out.