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?

FruitDiameterLabel
Fruit 155 mmmandarin
Fruit 259 mmmandarin
Fruit 363 mmmandarin
Fruit 471 mmorange
Fruit 577 mmorange
Fruit 683 mmorange

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.

Move the new fruit and each time look for the point closest to it. Between 63 and 71 mm it changes from blue to orange. Exactly halfway, at 67 mm, both would be equally far away.

Example

The 68 mm fruit

The machine works out the distance to each example and looks for the smallest one.

  1. To the smaller values: 68 − 55 = 13, 68 − 59 = 9, 68 − 63 = 5
  2. To the larger values: 71 − 68 = 3, 77 − 68 = 9, 83 − 68 = 15
  3. Smallest distance: 3 mm, to fruit 4 at 71 mm
  4. 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.

  1. The closest examples: 29 − 26 = 3, 26 − 22 = 4, 33 − 26 = 7, 26 − 17 = 9
  2. One neighbor: the newsletter with 29 links, so not spam
  3. Three neighbors: 29 not spam, 22 spam, 33 spam
  4. 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

A new case gets the label of the example whose value is closest to its own. If you ask the three nearest neighbors, the majority decides.

Tasks

Three tasks complete the lesson. The order does not matter, the text above stays where it is.

Task 1 · Order

A new case is to be classified by its three nearest neighbors. Put the steps in order.

Drag the rows into the right order.

  1. 1Measure the value of the new case
  2. 2Find the three smallest distances
  3. 3Take over the label of the majority
  4. 4Count the labels of these three examples
  5. 5Work out the distance to each example

Task 2 · Input

The new fruit measures 62 mm. What is the distance to its nearest neighbor?

Enter the result.

Distance =mm

Task 3 · Choice

The new fruit measures 75 mm. Which label does it get if the three nearest neighbors decide?

One answer is right.

Go deeper

Three more tasks, optional. They do not count for completing the lesson.

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?

Tap the line or drag the point to its place.

50556065707580

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?

Enter the result.

links

Task 6 · Several right · optional

Which statements about the nearest neighbor method are true?

Several answers are right. Pick all of them.

Back to the chapter