Skip to content

k-nearest neighbours

learning

Level 1 of 3

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.

fresh numbers: the questions a run answers, asked again on new numbers

choose one

question 1

A new point at 5 across, 4 up, among 2 8 t, 7 8 s, 6 2 c, 3 2 c and 8 5 s, with k = 3. How many distances are measured?

02468100246810
the answer, and why

the answer is D

One for each of the 5 points, before any vote.

if you chose another

  • That counts only the 3 that vote.
  • That counts every step of the run, the start, the ring, the vote and the verdict too.
  • That is the nearest point alone; it is found only after all 5 are measured.

question 2

On the same points, how many circles are inside the ring?

02468100246810
the answer, and why

the answer is C

The 3 nearest are the circles at 6, 2 and 3, 2, 2.24 and 2.83 away, and the square at 8, 5, 3.16 away.

if you chose another

  • There are only 2 circles among the points.
  • That leaves out the circle at 3, 2.
  • The nearest point is a circle.

question 3

On the same points, how far does the ring reach?

02468100246810
the answer, and why

the answer is D

It just reaches the farthest of the 3 nearest, the square at 8, 5, 3 across and 1 up, the root of 10.

if you chose another

  • That is the second nearest, the circle at 3, 2.
  • That is the square at 7, 8, the fourth nearest, outside the ring.
  • That is the farthest point of all, the triangle at 2, 8.

question 4

Which is true of k-nearest neighbours on n points?

the answer, and why

the answer is A

Every point is measured once before any vote, so the count depends on n alone.

this tests best, worst and average, on the learn page

if you chose another

  • k changes how many vote, not how many are measured.
  • Where the points stand changes nothing; each is measured once.
  • To know which point is nearest, every point must be measured.

pen and paper

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

  1. Write each point's distance from the new point.
  2. Which 3 are nearest, and what does the new point become?
  3. Count the distances, and check them against nn.
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 nn is 6.

Why does k-nearest neighbours measure exactly nn distances, on every set of points?
  1. the move: see the measuring

    the line:
    each point is measuredonce from the new point\begin{gathered} \text{each point is measured} \\ \text{once from the new point} \end{gathered}
    0246810024681054.472.242.83
    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.

  2. the move: one for each point

    the line:
    D=1+1+⋯+1D = 1 + 1 + \dots + 1

    why it is allowed: DD is the distances measured. By line 1, one for each of the nn points.

  3. the move: add the ones

    the line:
    D=nD = n

    why it is allowed: By line 2, nn ones add up to nn. For the four points, 4.

  4. the move: any set, any k

    the line:
    the same for every setof n points and every k\begin{gathered} \text{the same for every set} \\ \text{of } n \text{ points and every } k \end{gathered}

    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.

  5. the move: a ceiling

    the line:
    D≤nD \le n

    why it is allowed: By line 3, DD is nn, so it is at most nn.

  6. the move: read it as a bound

    the line:
    D∈O(n)D \in O(n)

    why it is allowed: By line 5, DD is at most 1 times nn.

  7. the move: a floor

    the line:
    D≥nD \ge n

    why it is allowed: By line 3, DD is at least nn too.

  8. the move: read it as a bound

    the line:
    D∈Ω(n)D \in \Omega(n)

    why it is allowed: By line 7, DD never drops below 1 times nn.

  9. the move: ceiling and floor together

    the line:
    D∈Θ(n)D \in \Theta(n)

    why it is allowed: Lines 6 and 8 bound DD by the same growth, nn.

try it on small inputs: over every set of 4 points, wherever they stand, the average is D=4D = 4

this tests where the formula comes from, on the learn page

what a full answer includes
  • The six distances.
  • The three nearest and the tie broken by the nearest.
  • Six distances matching nn.

question 6about 10 minutes

Take section 1's plane of three groups of six, with its three noisy points added.

  1. Why does k = 1 give 15 points the wrong class?
  2. Why does k = 3 give only 3 the wrong class?
  3. 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.

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

this tests how it works, on the learn page

what a full answer includes
  • The noisy point as the nearest of its neighbours.
  • Two votes against one.
  • A vote that reaches into other groups, and its ties.