demo · 02-ml-fundamentals

k-Nearest Neighbours — boundary in motion

Every background pixel is classified by majority vote among its k nearest training points. Slide k and watch the boundary morph from jagged memorisation at k=1 to smooth generalisation at k=15. Hover to see the neighbour lines drawn live.

Why it matters

kNN has no training phase — the entire dataset is the model. At prediction time it finds the k closest examples and takes a vote. That simplicity makes it the perfect lens for understanding the bias-variance tradeoff: k=1 has zero training error but high variance (every noise point creates a pocket in the boundary); large k smooths the boundary but can miss fine structure.

The math

d(p, q) = √((p₁−q₁)² + (p₂−q₂)²)    # Euclidean distance

ŷ(x) = majority_vote({y : (x,y) ∈ k-nearest(x)})

Anchored to 02-ml-fundamentals/supervised-learning.