Quant Memo
Foundational

K-Nearest Neighbors

The simplest possible way to predict something about a new point is to look at the points most like it already in your data and copy their answer, and almost every design choice in k-NN is about defining what 'most like it' means.

K-nearest neighbors (k-NN) predicts a new point's label by finding the kk most similar points already in the training data and having them vote. It doesn't fit a formula, doesn't estimate coefficients, and doesn't build a tree — at prediction time, it just measures distance to every stored training point and looks at whichever kk are closest. It is the closest thing machine learning has to "ask your neighbors."

The analogy: asking the houses on your street

If you wanted to guess a house's value and had no formula, a reasonable approach is asking the few most similar nearby houses — same size, same street, same age — what they're worth, and averaging their answers. You wouldn't ask a house across town in a different neighborhood; you'd trust the answer of the houses most like the one you're valuing. k-NN formalizes exactly that instinct: find the kk most similar examples, and let them answer for you.

Worked example: classifying a new point with k=3

Suppose training data has 6 points, each labeled A or B, plotted by two features (call them x1,x2x_1, x_2):

Pointx1x_1x2x_2Label
P111A
P221A
P312A
P455B
P565B
P656B

A new point arrives at (2,2)(2, 2). Compute Euclidean distance to each:

d(P1)=12+12=1.41, d(P2)=02+12=1.00, d(P3)=12+02=1.00d(P1) = \sqrt{1^2+1^2}=1.41,\ d(P2)=\sqrt{0^2+1^2}=1.00,\ d(P3)=\sqrt{1^2+0^2}=1.00 d(P4)=32+32=4.24, d(P5)=42+32=5.00, d(P6)=32+42=5.00d(P4)=\sqrt{3^2+3^2}=4.24,\ d(P5)=\sqrt{4^2+3^2}=5.00,\ d(P6)=\sqrt{3^2+4^2}=5.00

With k=3k=3, the three nearest are P2, P3 (tied at 1.00) and P1 (1.41) — all labeled A. The vote is unanimous, so the new point is classified A. Note that nothing was "trained" here; the entire computation happened at prediction time, scanning every stored point.

Decision boundary
flexibility 3.0points on wrong side 9reasonable

Try shrinking the neighborhood size in the explorer above until the boundary starts hugging individual training points tightly — that jagged, point-chasing boundary is what a small kk produces in k-NN, while a larger kk smooths the boundary by averaging over more neighbors, exactly like broadening the vote from 1 to 3 to 10 points in the worked example.

k-NN classifies a new point by majority vote among its kk closest training points under some distance metric, with no training phase at all — the entire "model" is the stored data plus the choice of kk and distance measure, both of which directly control how smooth or jagged the resulting decision boundary is.

What this means in practice

A small kk (like k=1k=1) produces a highly flexible, jagged boundary that can memorize noise in the training data; a large kk smooths predictions by averaging over more neighbors but can blur real, sharp distinctions between classes if kk gets too large relative to the data. Because every prediction requires scanning the training set for nearest neighbors, k-NN is also expensive at prediction time on large datasets, which is why it tends to be used on small-to-moderate datasets or paired with an indexing structure that speeds up the neighbor search.

The classic mistake is running k-NN on raw features with very different scales — say, a "price in dollars" feature ranging in the thousands next to a "return" feature ranging between -1 and 1 — where distance ends up dominated almost entirely by the large-scale feature, silently making the small-scale feature nearly irrelevant to every neighbor calculation. Features must be standardized (rescaled to comparable ranges) before computing distances, or the notion of "nearest" reflects unit choice rather than genuine similarity.

Related concepts

Practice in interviews

Further reading

  • Cover & Hart, Nearest Neighbor Pattern Classification (1967)
ShareTwitterLinkedIn