Skip to content
AI360Xpert
Beta
Core ML

Visual explainer

K-Nearest Neighbors

Predict a new point's class by looking at the k training points closest to it and taking a majority vote.

No model is learned, the training points are simply kept for later.
No model is learned, the training points are simply kept for later.

You never train anything here. The algorithm files every labelled point into memory and stops, with no weights and no boundary. When a newcomer lands, you open the archive and ask its neighbours.

Measure Every Gap

Five distances are measured, and 2.0 is the smallest of them.
Five distances are measured, and 2.0 is the smallest of them.

You ruler every stored point from the newcomer. Five gaps come back: 2.0, 2.24, 2.83, 3.16, 3.61. Nothing is approximated or skipped, which is why big archives answer slowly.

Three Vote

The three nearest vote two to one, so the newcomer is a square.
The three nearest vote two to one, so the newcomer is a square.

Draw the ring that holds exactly three neighbours: two squares and one circle. Democracy does the rest, and the square majority claims the newcomer two to one. That tally is your prediction.

K Decides

One neighbour says circle, three say square, so k decides the answer.
One neighbour says circle, three say square, so k decides the answer.

Same query, different k, different fate. With k at 1 the lone nearest circle wins outright. Widen to 3 and the two squares overturn it. Your knob setting is the verdict.

Votes Absorb Noise

Five votes drown one bad label, calling the newcomer a circle four to one.
Five votes drown one bad label, calling the newcomer a circle four to one.

One mislabelled square hides inside a circle crowd. A single neighbour would copy the error, but five votes bury it four to one. That forgiveness is the payoff for voting.

Tune K Carefully

Even counts can tie two to two, so keep k odd and moderate.
Even counts can tie two to two, so keep k odd and moderate.

Tiny k traces every stray dot, huge k washes the boundary flat. And even counts can deadlock two to two with no winner. So keep k odd, start at 3 or 5, and distrust regions where nearby k values disagree.

Where It Breaks

With hundreds of features every neighbour sits equally far away.
With hundreds of features every neighbour sits equally far away.

Give each point hundreds of features and distances converge: 3.58 through 3.62, tallest ahead by 0.04. Nearest becomes a coin flip, and voting degrades into luck. Shrink the space before you vote.

The Quick Version

  • No training happens, only storage.
  • Every gap gets measured exactly.
  • The k nearest take a majority vote.
  • Your k setting picks the winner.
  • Voting forgives one bad label.
  • Odd k avoids two to two ties.
  • Huge spaces make neighbours equidistant.