Specialty Digest

Global English editionLearners’ Study Dashboard — in development
The Knowledge Library for All

Flashcards: open the module flashcards (end-of-module review with workplace questions).

Online Courses / University Track / Learning from Data

Module 4 · Lesson 3 · Core

Nearest neighbours: classify by similar cases

Classify a new case by the labels of the k closest known cases.

  • 50 minutes
  • Serves O4
  • Assumes M4.L2

Phase 1: Prepare

Step 1 Warm-up recall

From M4.L2: how is a linear boundary found from a score?

Private: nothing you type here is sent or stored.

Step 2 Learning goal

By the end of this lesson you can:

  • compute a distance between two cases (Foundation);
  • classify by a vote of k neighbours (Core);
  • say how k changes the result (Core).

Phase 2: Teach

Step 3 A familiar example

A doctor facing an unusual case recalls the patients who looked most like it and what happened to them. The nearest-neighbours rule does the same thing with numbers.

Step 4 Clear explanation

The k-nearest-neighbours rule needs no fitted line. To classify a new case, measure its distance to every labelled case, take the k closest and give the new case the class most of them have.

A simple distance is the city-block distance: add the gap in each feature, like walking along streets. For (5, 5) and (4, 5) it is 1 + 0 = 1. The table shows six labelled cases and their distances from the new point (5, 5).

CasePoint (x, y)ClassCity-block distance from (5, 5)
A(4, 5)X1
B(5, 8)O3
C(8, 6)O4
D(2, 3)X5
E(9, 9)O8
F(0, 0)X10
k = 1: nearest is A, so X0k = 3: A, B, C vote X, O, O, so O1k = 5: A, B, C, D, E vote 2 X and 3 O, so O2Same new point, different k
Nearest neighbours: find the closest labelled cases and let them vote. Changing k can change the answer.

With k = 1 the nearest case, A, is class X, so the answer is X. With k = 3 the three nearest are A (X), B (O) and C (O), so the vote is two to one for O. With k = 5 the vote is three O to two X, again O. A single close neighbour can be a freak or a mistake, so a larger k smooths the result, but too large a k blurs real differences, and if k equals the whole training set the rule simply returns the majority class.

Neighbour rules depend on scale. If one feature is measured in thousands and another in single digits, the large one decides every distance, so features are usually rescaled first.

Step 5 Worked example (4 marks)

Three labelled cases are P (2, 2) class X, Q (6, 2) class O and R (3, 3) class X. Classify the new point (4, 2) with k = 1 and then k = 3, using city-block distance.

  1. Distances: P is 2 + 0 = 2; Q is 2 + 0 = 2; R is 1 + 1 = 2. (1 mark)
  2. All three tie at 2, so k = 1 cannot decide without a tie rule. (1 mark)
  3. With k = 3 all three vote: two X and one O, so the class is X. (1 mark)
  4. Answer: k = 1 is a tie; k = 3 gives X. (1 mark)

Go deeper: the professor's notes

Nearest neighbours need no fitted line: find the closest cases and let them vote.
Distance

City-block.

k

Voters.

Scale

Rescale.

New casepointDistancesto allClosest kvoteClassmajority
Neighbours

Votes

kNeighboursClass
1XX
3X, O, OO
52 X, 3 OO

At work: Same point

The answer changes with k.

  1. Compute distances.
  2. Vote among the k closest.

What a careful practitioner decides. Pick k on cases not used to fit.

Phase 3: Practise

Step 6 Learner practice (instant-marked)

1. In k-nearest neighbours, k is the:

2. The city-block distance from (1, 1) to (4, 5) is:

3. Neighbours X, O, O with k = 3 give:

4. One feature measured in thousands can dominate distances unless:

Phase 4: Close

Step 7 Recap

  • k-nearest neighbours classifies by a vote of the k closest labelled cases.
  • Distance depends on feature scale, so rescale first.
  • Small k follows noise; large k blurs differences.

Step 8 Exit ticket (ungraded)

Neighbours at distances 1, 2 and 3 have classes O, X and X. What class does k = 3 give?

Private: nothing you type here is sent or stored.

Step 9 Go further (Extension)

Think of a case in your work where finding the most similar past cases would help a decision.

In industry: Minsky and Papert: what one line cannot do

In 1969 Marvin Minsky and Seymour Papert published Perceptrons, showing that a single-layer perceptron can only learn patterns that one straight line can separate. The exclusive-or pattern, where the class is Yes only when exactly one of two inputs is 1, is the standard example it cannot learn.

Link to this lesson. Linear separability is a real limit, and checking it is part of choosing a classifier.

Source: Minsky and Papert (1969), Perceptrons; Perceptron and Perceptrons (book), Wikipedia

Common mistakes

MistakeWhy it fails
Not rescaling featuresLarge-valued features dominate the distance.
Using k = 1 on noisy dataOne odd neighbour decides the answer.
Ignoring tiesDecide a tie rule in advance, such as an odd k.

Limits. Neighbour rules need all the labelled data at prediction time and slow down as the data grow.

Flashcards for this lesson (3)

Tap a card to reveal the answer. Read the question, answer it aloud or in writing first, then check yourself against the explanation.

Card 1 of 3 · How do you score a linear rule?
Right7All8Accuracy87.5%
Scoring

Classify every known case and count the wrong ones. Seven right out of eight is 87.5 percent, against a baseline of 62.5 percent.

Card 2 of 3 · What is k-nearest neighbours?
Distancesto allClosest kvoteMajoritywins
k-NN

Classify a new case by the classes of the k closest labelled cases, using a majority vote. It needs no fitted line.

Card 3 of 3 · What is city-block distance?
Gap in x3Gap in y1Total4
Distance

The sum of the gaps in each feature. From (5, 5) to (4, 5) it is 1; from (5, 5) to (8, 6) it is 4.

Review with flashcards

Test yourself on this module with workplace flashcards and detailed answers.

Open the module flashcards