MoonSay /Tutorials/Machine Learning Interview #10
Contents
  1. 01Introduction
  2. 02The space explodes
  3. 03Your neighbors drift away
  4. 04The orange is all peel
  5. 05More features, worse model
  6. 06Why machine learning still works
  7. 07Common misconceptions
  8. 08Interview questions and model answers
  9. 09Summary
  10. References

03Your neighbors drift away

3.1The edge of a neighborhood

Local methods such as k-nearest neighbors predict from the training points near the query. Suppose we want a neighborhood that holds 10\% of the data. In one dimension, with uniform data on [0,1], an interval of length 0.1 will do. In d dimensions, a sub-cube with edge e has volume e^d, so holding a fraction r of uniform data needs e^d=r:

e_d(r)=r^{1/d}. \tag{3}

For r=0.1 the edge is 0.32 in two dimensions, 0.46 in three, 0.79 in ten and 0.98 in a hundred (Hastie et al., 2009, §2.5). In ten dimensions the “neighborhood” must span 79\% of the range of every feature; ESL rounds this to 0.80. Even 1\% of the data needs an edge of 0.63 in ten dimensions (Figure 2b). Such neighborhoods are not local any more: the prediction averages points that differ a lot from the query, which is bias in the language of Episode 2 (Murphy, 2022, §16.1.2).

(a) 700 uniform points in the square; the box of side 0.32=\sqrt{0.1} holds exactly 70 of them, 10\% (in three dimensions the box of side 0.46 also holds exactly 70). (b) The edge length (3) of a box that holds a fraction r of the data, against the number of features. Computed for this tutorial, with the same data and seed as the companion video.
Figure 2. (a) 700 uniform points in the square; the box of side 0.32=\sqrt{0.1} holds exactly 70 of them, 10\% (in three dimensions the box of side 0.46 also holds exactly 70). (b) The edge length (3) of a box that holds a fraction r of the data, against the number of features. Computed for this tutorial, with the same data and seed as the companion video.
Try itHow big is a neighborhood?

Choose the number of features and the share of the data a neighborhood should hold. Left: the box that holds it, shown as its extent on each feature (blue). Right: its edge e_d(r)=r^{1/d}, equation (3), and the median distance to the nearest of N points in the unit ball, equation (4), against the number of features.

3.2How far is the nearest neighbor?

A second view looks at the closest point itself. Place N points uniformly in the unit ball of \mathbb{R}^d and stand at the center. A single point lands within radius \rho with probability \rho^d, the volume ratio of the two balls, so all N points miss that ball with probability (1-\rho^d)^N. Setting this to \tfrac12 gives the median distance to the nearest point (Hastie et al., 2009, eq. 2.24):

\operatorname{median}D_{\min}=\Bigl(1-\bigl(\tfrac12\bigr)^{1/N}\Bigr)^{1/d}. \tag{4}

With N=500 it is 0.0014 for d=1, 0.037 for d=2, 0.11 for d=3, 0.52 for d=10 and 0.94 for d=100. In ten dimensions the nearest of 500 points is typically more than halfway to the boundary of the ball. Since (1/2)^{1/N}\approx1-\ln2/N, the median is about (\ln2/N)^{1/d}: halving it takes 2^d times more data.

Pause and think. A 5-nearest-neighbor model is trained on 10{,}000 points with 20 features, roughly uniform. What fraction of the data does a neighborhood hold, and what edge length does a box holding it need?

Show the answer

5 of 10{,}000 is r=0.0005, and e_{20}(0.0005)=0.0005^{1/20}=0.68: the five “nearest” neighbors spread over about two thirds of the range of every feature. The model averages far-away points, so its predictions are biased toward the global average.