K-Nearest Neighbors
K-Nearest Neighbors — K-nearest neighbors is a supervised learning algorithm that classifies a point by a majority vote of its k closest labeled examples under a distance metric. It does no training — it simply stores the data and measures distance at prediction time.
Want to classify something? Just look at the closest examples you’ve already seen and go with the majority. That’s KNN — no training needed, just memory and a sense of distance.
- Class A
- Class B
- Query point
The 5 nearest, sorted (hover to locate, click to scrub)
KNN controls
The idea in plain words
KNN doesn’t train — it memorizes the data. To classify a new point, it looks at the k closest labeled examples and takes a majority vote. Drag the query point around and watch its predicted class flip as its neighborhood changes.
With k = 1 the boundary bends around every noisy point (overfitting); with k as large as the dataset it always returns the global majority (underfitting). It’s a useful contrast to a fitted model like linear regression.
Now, the math
Neighbors are ranked by Euclidean distance:
- two points being compared.
- the j-th feature (coordinate) of point p.
- how many nearest neighbors vote.
▸ Show the derivation
k controls the bias–variance balance: small k gives a flexible, high-variance boundary that chases noise; large k averages over a wide neighborhood, raising bias until the model ignores local structure entirely.
Trace it by hand
Classify the query q = (3, 3) against five labeled points: (4,4) B, (5,3) A, (1,3) A, (2,5) B, (6,6) B. Distances are Euclidean, rounded to 3 decimal places.
Distance to the first point
Distances to the other four
Vote with k = 3
The single closest point is a B, but the vote overrules it.
Vote with k = 5
Same query, same data — only k changed, and the answer flipped.
What just happened: Five square roots and two votes: k = 3 predicts A while k = 5 predicts B for the very same query. The choice of k is not a detail — it decides the answer near class borders.
Now Break It
Try this: k=1 memorizes every noisy point; k=N always predicts the majority class regardless of position.
Control: k slider (set to 1, then to maximum)
What happens: k=1: Memorizing noise — the boundary is jagged and overfitting. k=max: Ignoring all structure — predicting the majority class everywhere.
Where k-nearest neighbors is used
K-nearest neighbors classifies a new point by looking at the labels of its closest training examples and taking a majority vote, which makes it a natural fit for problems where similar inputs should share an outcome. Recommendation systems use neighbor search to suggest items enjoyed by people with similar tastes. Content-based image and document retrieval find the most similar examples to a query, and modern semantic search over vector embeddings is essentially nearest-neighbor lookup at massive scale. It also appears in anomaly detection, where a point far from all its neighbors is flagged as unusual, and in medical decision support, where clinicians surface historically similar cases. Because it stores the data rather than fitting parameters, it adapts instantly whenever new labeled examples arrive.
The biggest misconception is that k-nearest neighbors does no training, so it must be cheap. Training is trivial, but every prediction requires comparing the query against many stored points, which makes inference slow and memory-hungry on large datasets unless you use spatial indexes or approximate search. A second pitfall is ignoring feature scaling: because the method relies on distances, a feature measured in thousands will dominate one measured in fractions, so standardization is essential. People also underestimate the curse of dimensionality, where in high dimensions all points become roughly equidistant and the notion of nearest loses meaning. Finally, choosing k too small makes predictions noisy while choosing it too large blurs genuine class boundaries.
Frequently asked questions
How do I choose the value of k?
Why is feature scaling so important for KNN?
Is KNN really training-free?
What is the curse of dimensionality in this context?
Can KNN be used for regression too?
What distance metric should I use?
Written & reviewed by the ML Visualization team · Last updated .