Qm
Core

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 kk closest training points to a new query and combining their answers. Two choices decide almost everything: how many neighbors, kk, to consult, and whether all kk 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 k=1k=1, the prediction is just the single nearest point's label, low bias, high variance. As kk 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:

y^(x)=iNk(x)wiyiiNk(x)wi,wi=1d(x,xi).\hat y(x) = \frac{\sum_{i \in N_k(x)} w_i \, y_i}{\sum_{i \in N_k(x)} w_i}, \qquad w_i = \frac{1}{d(x, x_i)} .

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 kk-neighborhood barely counts. This lets you safely use a larger kk, 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 +1.2%,+0.8%,0.3%,+2.0%,+0.5%+1.2\%, +0.8\%, -0.3\%, +2.0\%, +0.5\%, distances 0.1,0.2,0.4,1.5,1.80.1, 0.2, 0.4, 1.5, 1.8. Unweighted average: 0.84%0.84\%, treating the two distant, less-relevant days the same as the two close ones. Weighting by 1/di1/d_i gives weights 10,5,2.5,0.67,0.5610, 5, 2.5, 0.67, 0.56, and a weighted average of

10(1.2)+5(0.8)+2.5(0.3)+0.67(2.0)+0.56(0.5)10+5+2.5+0.67+0.561.10%,\frac{10(1.2)+5(0.8)+2.5(-0.3)+0.67(2.0)+0.56(0.5)}{10+5+2.5+0.67+0.56} \approx 1.10\%,

leaning more heavily toward the two very close analogues and discounting the two distant, weaker ones.

query circle size = vote weight (1/distance)
The two closest neighbors (large circles) dominate the weighted vote; the two farthest (tiny circles) barely register, even though all five are inside the k=5 neighborhood.

What this means in practice

Choose kk (and whether to weight) by cross-validation, not a rule of thumb. Larger kk with distance weighting is usually safer than small unweighted kk: it protects against one noisy neighbor dominating while still letting the closest points carry most of the weight. Always tune kk jointly with the distance metric, a poorly-scaled metric can put irrelevant "near" neighbors ahead of genuinely relevant ones regardless of kk.

Increasing kk 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 kk is often more robust than a small unweighted one.

Don't tune kk by eyeballing training error, a small kk can perfectly memorize training data and look flawless there while generalizing terribly. Always select kk using held-out or cross-validated error.

Discussion

Sign in to join the discussion · reading is open to everyone

💡 Discussion rules

  1. Ask and answer about this concept. Off-topic gets removed.
  2. No homework dumps. Show what you tried first.
  3. Corrections are welcome. Cite a source when you claim an error.

Loading discussion…

Related concepts

Practice in interviews

Further reading

  • Hastie, Tibshirani & Friedman, The Elements of Statistical Learning, ch. 13.3
ShareTwitterLinkedIn