Lesson 3 of 5 · Open
The nearest neighbor
A learning method you can work through by hand.
A sorting machine at a wholesale market measures the diameter of every fruit that rolls along the belt. It has stored six fruits as examples, each with its label: mandarin or orange. Now a new fruit comes along at 68 mm. Nobody has entered the diameter from which a fruit counts as an orange. How does the machine get to an answer?
| Fruit | Diameter | Label |
|---|---|---|
| Fruit 1 | 55 mm | mandarin |
| Fruit 2 | 59 mm | mandarin |
| Fruit 3 | 63 mm | mandarin |
| Fruit 4 | 71 mm | orange |
| Fruit 5 | 77 mm | orange |
| Fruit 6 | 83 mm | orange |
Similar values, same label
The diameter is the variable the machine compares by. In the language of machine learning, a variable is often called a feature. The idea: fruits with a similar diameter are often of the same kind. So the machine looks for the example that is most similar to the new fruit, and takes over its label.
How similar two fruits are is measured by the distance: the difference of their values, that is, the larger one minus the smaller one. So a distance is never negative. The example with the smallest distance is called the nearest neighbor. The new fruit gets its label.
On the number line below, each example is a point at the place of its diameter. The two colors are the two labels: blue for mandarin, orange for orange. The point with the question mark is the new fruit. It has no label yet, so its color does not matter.
Example
The 68 mm fruit
The machine works out the distance to each example and looks for the smallest one.
- To the smaller values: 68 − 55 = 13, 68 − 59 = 9, 68 − 63 = 5
- To the larger values: 71 − 68 = 3, 77 − 68 = 9, 83 − 68 = 15
- Smallest distance: 3 mm, to fruit 4 at 71 mm
- Take over the label: the new fruit is an orange.
Quick check
What does the new fruit take over from its nearest neighbor?
Typical mistake
Looking to one side only, usually to the next smaller value: below 68 mm, the next fruit is the mandarin at 63 mm, so the new fruit would be a mandarin. That cannot be right: the orange at 71 mm is only 3 mm away, the mandarin 5 mm. The nearest neighbor can lie on either side.
Asking three neighbors
A single example can be unusual or carry a wrong label. If it happens to be the closest, it decides alone. That is why people often ask the three nearest neighbors and take the label that the majority of them have. With two labels and three neighbors there is always a majority: at least two have the same label.
This means the three nearest, not all examples. The majority of all examples would be the same for every new case, no matter what value it has.
New terms
- distance
- the difference of two values: the larger one minus the smaller one
- nearest neighbor
- the example with the smallest distance to the new case
- majority of the neighbors
- the label that most of the three nearest neighbors have
Example
In everyday life: links in emails
A spam filter compares emails by the number of links in them. Seven examples are stored. One of them is a newsletter someone signed up for, with 29 links: not spam. The new email has 26 links.
- The closest examples: 29 − 26 = 3, 26 − 22 = 4, 33 − 26 = 7, 26 − 17 = 9
- One neighbor: the newsletter with 29 links, so not spam
- Three neighbors: 29 not spam, 22 spam, 33 spam
- Majority: spam twice, not spam once. The new email gets the label spam.
Where the model is here
This method has no training in the narrow sense. The machine does not look for a pattern beforehand, it only stores the examples. The model is the examples themselves, and the calculating only starts when a new case comes in.
Real systems usually compare many variables at once, for fruit perhaps diameter, weight, and color. The distance is then calculated from all variables together. The idea stays the same: a new case gets the label of the examples most similar to it.
Remember
Tasks
Task 1 · Order
A new case is to be classified by its three nearest neighbors. Put the steps in order.
- 1Measure the value of the new case
- 2Find the three smallest distances
- 3Take over the label of the majority
- 4Count the labels of these three examples
- 5Work out the distance to each example
Task 2 · Input
The new fruit measures 62 mm. What is the distance to its nearest neighbor?
Task 3 · Choice
The new fruit measures 75 mm. Which label does it get if the three nearest neighbors decide?
Go deeper
Task 4 · Number line · optional
Only a mandarin at 56 mm and an orange at 74 mm are stored. Where is a new fruit equally far from both?
Task 5 · Input · optional
A new email has 19 links. Its nearest neighbor has more links, at a distance of 4. How many links does it have?
Task 6 · Several right · optional