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).
| Case | Point (x, y) | Class | City-block distance from (5, 5) |
|---|---|---|---|
| A | (4, 5) | X | 1 |
| B | (5, 8) | O | 3 |
| C | (8, 6) | O | 4 |
| D | (2, 3) | X | 5 |
| E | (9, 9) | O | 8 |
| F | (0, 0) | X | 10 |
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.
- Distances: P is 2 + 0 = 2; Q is 2 + 0 = 2; R is 1 + 1 = 2. (1 mark)
- All three tie at 2, so k = 1 cannot decide without a tie rule. (1 mark)
- With k = 3 all three vote: two X and one O, so the class is X. (1 mark)
- Answer: k = 1 is a tie; k = 3 gives X. (1 mark)
Go deeper: the professor's notes
City-block.
Voters.
Rescale.
Votes
| k | Neighbours | Class |
|---|---|---|
| 1 | X | X |
| 3 | X, O, O | O |
| 5 | 2 X, 3 O | O |
At work: Same point
The answer changes with k.
- Compute distances.
- 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:
How many close cases are used.
2. The city-block distance from (1, 1) to (4, 5) is:
3 + 4.
3. Neighbours X, O, O with k = 3 give:
Two to one for O.
4. One feature measured in thousands can dominate distances unless:
Put features on comparable scales.
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
| Mistake | Why it fails |
|---|---|
| Not rescaling features | Large-valued features dominate the distance. |
| Using k = 1 on noisy data | One odd neighbour decides the answer. |
| Ignoring ties | Decide 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.