Also called k-NN and Nearest neighbour classification.
test yourself
Six questions, four to pick from and two for paper. A point such as 2 8 t is a triangle at 2 across, 8 up (s is a square, c a circle). Each answer stays hidden until you open it.
Bk changes how many vote, not how many are measured.
CWhere the points stand changes nothing; each is measured once.
DTo know which point is nearest, every point must be measured.
pen and paper
5question 5about 12 minutes
Six points, 1 1 t, 6 5 s, 3 7 c, 5 2 c, 8 8 s and 2 4 t. A new point at 4 across, 4 up, with k = 3.
Write each point's distance from the new point.
Which 3 are nearest, and what does the new point become?
Count the distances, and check them against .
a hint
Equally near, the point listed first in the question comes first. A tie between shapes goes to the nearest of them.
a worked answer
The square at 6, 5 is 2 across and 1 up: the root of 4 + 1 = 5, about 2.24. The distances are 4.24, 2.24, 3.16, 2.24, 5.66 and 2. The 3 nearest are the triangle at 2, 4, 2 away, then the square at 6, 5 and the circle at 5, 2, both 2.24 away. One triangle, one square and one circle: a tie, so the nearest of them decides, and the new point becomes a triangle. The run measured 6 distances, and is 6.
Why does k-nearest neighbours measure exactly distances, on every set of points?9 of 9 shown
1the move: see the measuring
the line:
Four points and a new one at 5 across, 4 up. Each point's distance is written beside it, 5, 4.47, 2.24 and 2.83. Four distances.
why it is allowed: The run measures every point in turn, once, before any vote. The picture measures four.
2the move: one for each point
the line:
why it is allowed: is the distances measured. By line 1, one for each of the points.
guess it, then
3the move: add the ones
the line:
why it is allowed: By line 2, ones add up to . For the four points, 4.
guess it, then
4the move: any set, any k
the line:
why it is allowed: In line 1, how many points are measured never depends on where they stand or on k, only on their being there.
guess it, then
5the move: a ceiling
the line:
why it is allowed: By line 3, is , so it is at most .
guess it, then
6the move: read it as a bound
the line:
why it is allowed: By line 5, is at most 1 times .
guess it, then
7the move: a floor
the line:
why it is allowed: By line 3, is at least too.
guess it, then
8the move: read it as a bound
the line:
why it is allowed: By line 7, never drops below 1 times .
guess it, then
9the move: ceiling and floor together
the line:
why it is allowed: Lines 6 and 8 bound by the same growth, .
guess it, then
try it on small inputs: over every set of 4 points, wherever they stand, the average is
The three nearest and the tie broken by the nearest.
Six distances matching .
6question 6about 10 minutes
Take section 1's plane of three groups of six, with its three noisy points added.
Why does k = 1 give 15 points the wrong class?
Why does k = 3 give only 3 the wrong class?
Why does k = 15 give more again, 9?
a hint
Each noisy point stands inside a group of another shape.
a worked answer
With k = 1 each held-out point takes its single nearest point's shape. Each noisy point is the nearest point of four points round it, and they get its wrong shape: 12, and the 3 noisy points themselves, 15 in all. With k = 3 two group points outvote the one noisy point, so only the 3 noisy points, whose neighbours are all of another shape, are wrong. With k = 15 a held-out point's own shape gets at most 6 of the 15 votes: the 5 others of its group and a noisy point of its shape. The triangles at 2, 9 and 3, 8 get 6 squares, 5 triangles and 4 circles. The circles at 3, 2 and 2, 3 get 7 triangles, 5 circles and 3 squares. The circles at 4, 1 and 4, 3 get 6 triangles, 6 circles and 3 squares, a tie. The nearest point of the tied shapes, the noisy triangle at 4, 2, decides. Those 6 points and the 3 noisy points: 9.
The plane with its noise added. A square stands among the triangles at 2, 8, a circle among the squares at 8, 7, and a triangle among the circles at 4, 2.