k-Nearest Neighbours

18 min read

Tell me your neighbours and I will tell you who you are. The k-nearest neighbours method (kNN) puts a new item in the group of its closest examples. There is no training step. In this lesson you build it in a few lines of NumPy.

New words

Neighbour
A known example that lies close to the new item.
Distance
A number that tells how far apart two items are.
k
How many neighbours we ask. We choose it ourselves.
Majority vote
The group with the most neighbours wins.
Feature scaling
Changing features so that they all use a similar range.
Lazy learner
A method that does no training. It just stores the data.

What you will learn

  1. Which fruit is this?
  2. How do you measure distance?
  3. Who are the nearest neighbours?
  4. How does the vote work?
  5. Which k is best?
  6. Why must features be on the same scale?
  7. Why is kNN slow at prediction time?
  8. Try it: place a new fruit
  9. Common mistakes
  10. Explain it to a friend
  11. Key takeaways

10 quiz questions at the end.

Start this lesson

The full lesson, with interactive demos and the quiz, opens after you sign in with Google. It is free.

All lessons · Back to the roadmap