Choosing k and Distance Weighting in k-NN
The two knobs that make or break a k-nearest-neighbors model: how many neighbors to consult, and whether every neighbor gets an equal vote or closer ones count for more. Both trade off smoothness against sensitivity to genuine local structure.
Prerequisites: K-Nearest Neighbors
k-NN predicts by looking at the closest training points to a new query and combining their answers. Two choices decide almost everything: how many neighbors, , to consult, and whether all get an equal say or the closest ones are trusted more. Get these wrong and k-NN either chases noise or smooths away the local structure that made it worth using.
The analogy: how many neighbors to ask, and how loudly
Ask one neighbor about a new restaurant and you get a fast, specific answer — but if they have unusual taste, you're stuck with their quirk. Ask fifty and you get a stable average — but if you live on a particularly foodie block, that average gets diluted by opinions from people who aren't really representative. Distance weighting is the compromise: ask fifty, but let the ones next door speak louder than the ones three streets over.
The mechanics: k controls smoothness, weighting controls fairness by distance
With , the prediction is just the single nearest point's label — low bias, high variance. As grows, the prediction averages over more points, smoothing the boundary but risking bias once the neighborhood includes points that aren't really local. A distance-weighted vote replaces the plain average:
In plain English: each neighbor's vote is divided by its distance from the query, so a neighbor right next to the query dominates while one at the far edge of the -neighborhood barely counts. This lets you safely use a larger — cutting noise — without losing sensitivity to the truly closest points.
Worked example: five neighbors, weighted and unweighted
Five nearest historical days to today, next-day returns , distances . Unweighted average: — treating the two distant, less-relevant days the same as the two close ones. Weighting by gives weights , and a weighted average of
leaning more heavily toward the two very close analogues and discounting the two distant, weaker ones.
What this means in practice
Choose (and whether to weight) by cross-validation, not a rule of thumb. Larger with distance weighting is usually safer than small unweighted : it protects against one noisy neighbor dominating while still letting the closest points carry most of the weight. Always tune jointly with the distance metric — a poorly-scaled metric can put irrelevant "near" neighbors ahead of genuinely relevant ones regardless of .
Increasing trades variance for bias by averaging over a larger neighborhood; distance weighting recovers local sensitivity by letting the closest points dominate the vote even within a large neighborhood — a large weighted is often more robust than a small unweighted one.
Don't tune by eyeballing training error — a small can perfectly memorize training data and look flawless there while generalizing terribly. Always select using held-out or cross-validated error.
Related concepts
Practice in interviews
Further reading
- Hastie, Tibshirani & Friedman, The Elements of Statistical Learning, ch. 13.3