2.3.17Tree-Based & Instance Methods

Choosing K and the curse of dimensionality

1,827 words8 min readdifficulty · medium6 backlinks

Context: In k-Nearest Neighbors (kNN) we classify a point by the majority vote (or average) of its kk closest training points. Two questions decide whether kNN works at all: How big should kk be? and Why does kNN quietly die in high dimensions?

1. The role of kk — bias vs variance

WHY small kk = high variance

With k=1k=1, your prediction is one training label. If that neighbor happens to be a mislabeled or noisy point, you copy the error. Perturb the training set slightly → the nearest neighbor can flip → prediction flips. High sensitivity to data = high variance.

WHY large kk = high bias

As kNk \to N (all points), every query returns the same answer: the global majority class / global mean. The model ignores xx entirely. That systematic wrongness is bias.


2. The curse of dimensionality

Figure — Choosing K and the curse of dimensionality

WHY neighborhoods explode — the volume argument

Suppose data is uniform in the unit cube [0,1]d[0,1]^d. To capture a fraction rr of the data with a small cube of side \ell, we need the volume to match: d=r    =r1/d.\ell^d = r \;\Rightarrow\; \ell = r^{1/d}.

WHY distances concentrate

For random points in dd dimensions, the ratio between the farthest and nearest neighbor distances shrinks toward 1: dmaxdmindmind0.\frac{d_{\max} - d_{\min}}{d_{\min}} \xrightarrow{d\to\infty} 0. Each coordinate adds a roughly independent squared term to xy2=i=1d(xiyi)2\|x-y\|^2 = \sum_{i=1}^d (x_i-y_i)^2. By the law of large numbers the average term stabilizes, so all pairwise distances cluster near the same value — their relative spread vanishes. If everything is equidistant, "the kk nearest" is essentially random.


3. Practical prescription (the 80/20)

  • Standardize features first (z-score), else large-scale features hijack distance.
  • Choose kk by cross-validation, expect a U-curve; use 1-SE rule for robustness.
  • Reduce dd before kNN: drop irrelevant features, PCA, or embeddings.
  • Rule of thumb: kNN shines when dd is small (say 15\lesssim 15) and NN is large.
Recall Feynman: explain to a 12-year-old

Imagine finding friends by "who lives closest." On a single street (1D), your closest neighbor is genuinely next door — easy. Now imagine everyone lives in a giant 20-dimensional apartment building where each person differs on 20 traits. Suddenly everybody is "sort of similar and sort of different" — nobody is clearly your closest friend. That's the curse: in many dimensions, closeness stops being meaningful, so "ask your nearest neighbor" gives useless advice. And kk? It's how many neighbors you ask: ask one and a weird person misleads you; ask everyone and you just get the crowd's average, ignoring you completely. Pick a middle number by testing.

Flashcards

What does kk control in kNN, in bias–variance terms?
Small kk = high variance (overfit); large kk = high bias (underfit). It's a smoothing knob.
Approximate degrees of freedom of kNN?
dfN/k\text{df}\approx N/k; more neighbors → fewer effective regions → simpler model.
How do you choose kk in practice?
Cross-validation, pick the kk minimizing CV error (U-shaped curve); 1-SE rule for a simpler, robust choice.
Edge length to capture fraction rr of uniform data in dd dims?
=r1/d\ell = r^{1/d}; grows toward 1 as dd increases.
For r=0.01r=0.01, d=10d=10, what is \ell?
0.010.10.630.01^{0.1}\approx 0.63 — must span 63% of each axis, so "local" is meaningless.
Why do distances concentrate in high dimensions?
xy2=i(xiyi)2\|x-y\|^2=\sum_i(x_i-y_i)^2; many independent terms average out, so all pairwise distances cluster near one value; relative spread → 0.
How does required sample size scale with dimension for fixed density?
Exponentially, NbdN\propto b^d.
Why doesn't "add more features" help kNN like it can help linear models?
kNN has no coefficients; each feature adds equally to distance, so irrelevant features inject noise into every neighbor computation.
Is picking odd kk a universal rule?
No — only avoids ties in binary problems; with >2 classes ties still occur. Use distance weighting / CV instead.

Connections

Concept Map

vote of

small k

large k

jagged boundary

blurs structure

measured by

tuned via

U-shaped error

refined by

breaks in

makes points

nearest not close

k-NN rule

k neighbors

High variance / overfit

High bias / underfit

Bias-variance tradeoff

df approx N over k

5-fold cross-validation

Optimal k

1-SE rule

Curse of dimensionality

Nearly equidistant

Hinglish (regional understanding)

Intuition Hinglish mein samjho

kNN me do cheezein sabse important hain: kk kitna rakhein aur high dimensions me kya hota hai. kk ek smoothing knob samjho. Agar k=1k=1 rakha, to model sirf ek closest neighbor ki baat maanega — agar wahi point galti se noisy ya galat labelled hua, prediction bhi galat. Isko high variance bolte hain (overfitting). Agar kk bahut bada, model bas majority class ya average de dega, tumhare point xx ko ignore karke — ye high bias (underfitting) hai. Isliye beech ka kk cross-validation se choose karte hain; error vs kk ka graph U-shape banta hai, aur U ke bottom pe best kk milta hai.

Ab curse of dimensionality. Formula yaad rakho: uniform data me sirf fraction rr pakadne ke liye har axis pe edge length =r1/d\ell = r^{1/d}. Maano d=10d=10 aur tum sirf 1% data chahte ho — =0.010.10.63\ell = 0.01^{0.1} \approx 0.63, matlab har axis ka 63% span cover karna padega! To "nearest neighbor" ab local raha hi nahi. High dimensions me saare points ki aapas ki distance almost barabar ho jaati hai, isliye "nearest" ka matlab hi khatam.

Practical baat: pehle features ko standardize karo (z-score), warna bade scale wala feature distance ko hijack kar lega. Phir agar dimensions zyada hain to PCA ya feature selection se kam karo — kyunki kNN me har feature distance me equally add hota hai, irrelevant features sirf noise dalte hain. Ek common galti: "odd kk lo taaki tie na ho" — ye sirf 2-class me kaam ka hai, 3+ classes me phir bhi tie ho sakta hai. So bharosa cross-validation pe rakho, rule-of-thumb pe nahi.

Test yourself — Tree-Based & Instance Methods

Connections