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.
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
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
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
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
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
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
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.