Skip to content

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

02468100246810

Test points

02468100246810
Ten training points and ten test points, with k at 1. Each plane is shaded by the shape of the nearest training point. The rule gets none of its own points wrong. It gets 4 test points wrong, each ringed, three of them beside an odd point.

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.

ktraining points wrongtest points wrong
104
204
311
412
534
634
745
846
947
Both errors at each k, out of 10 points. The training error only stays level or climbs. The test error is lowest at k = 3.

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

02468100246810

Test points

02468100246810
0246123456789kpoints wrongtraining points wrongtest points wrong

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.

The same points with k free to change, starting at 3. Watch the shading round the two odd points as k grows.

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 nn, 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.

question 1You have nn points, a multiple of 5, and hold back a fifth of them as test points. How many training points, TT, are left?
  1. the move: take the test points away

    the line:
    T=n−n5T = n - \frac{n}{5}

    why it is allowed: A fifth of the points is nn divided by 5. The training points are the rest.

  2. the move: write n in fifths

    the line:
    T=5n5−n5T = \frac{5n}{5} - \frac{n}{5}
    pointstest points, a fifthtraining points
    1028
    20416
    501040
    1002080
    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 nn make nn, so both parts now stand over 5.

  3. the move: take one fifth from five

    the line:
    T=4n5T = \frac{4n}{5}

    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.

try it with a small nn: at n=10n = 10, line 1 gives T=8T = 8, 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?

  1. On the practice questions Ana scores 10 and Ben scores 9. Judged there, Ana wins.
  2. On the new questions Ana scores 4 and Ben scores 8. Judged there, Ben wins.
  3. 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.
studentpractice questions rightnew questions rightdrop
Ana1046
Ben981
Each student's score on the practice questions and on the new ones, out of 10. The practice questions are the training points, and the new questions the test points.

the trap

check yourself

  1. 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.

  2. 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.

  3. 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.