Skip to content
BENIBIX Learn

k-Nearest Neighbors (k-NN)

AI📈 Machine LearningClassic ML Algorithms

Also known as k-NN, KNN, Nearest Neighbor Classifier

It finds the closest cases it knows. Then it copies what most of them say.

Think of it like

Think of it like a new kid at school. You guess their team from the three kids next to them.

Example

Jack sets k to 3. A new fruit sits nearest to two apples and one pear on his chart, so his model calls it an apple.

Why it matters

It is simple and needs no real training. The same idea of finding the nearest items helps power search and suggestions.

Definition

k-nearest neighbors answers by looking at the closest examples it has stored. For a new example, it finds the k closest ones in the training data, where k is a small number you pick, like 5. If most of those neighbours are in one group, the new example goes in that group too. It does almost no work during training. All the work happens when a question comes in.
With k set to 3, the circle holds the three nearest dots. Two are coloured, so the new dot is called coloured.

Common mistake

Using it on huge data with no help. Checking each new case against millions of stored ones is slow.

Key takeaways

  1. 1
    Find the closest. The k nearest examples.
  2. 2
    Copy the majority. Most neighbours decide.
  3. 3
    Lazy learner. Work happens at question time.

Compare

Learn it properly