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

05More features, worse model

5.1An experiment on real digits

Now let us watch the curse hurt a model. We use the 1{,}797 handwritten digits of Alpaydin and Kaynak (1998), 8\times8 images with pixel values 0 to 16, split into 1{,}257 training and 540 test images with the same share of each digit. A 1-nearest-neighbor classifier with Euclidean distance recognizes 98.3\% of the test digits. Then, following the thought experiment of Domingos (2012), we add features that carry no information: each digit is centered on a larger canvas filled with random pixels, uniform on [0,16] and drawn afresh for every image. The digit itself is untouched; only noise is added.

canvas8\times812\times1216\times1624\times2432\times32
features641442565761{,}024
1-nearest-neighbor accuracy98.3\%93.0\%83.3\%67.2\%55.2\%
median D_{\max}/D_{\min}3.911.631.351.191.13
Table 1. Real digits padded with noise pixels. The last row is the median, over test images, of the distance to the farthest training image divided by the distance to the nearest. Computed for this tutorial, with the same data and seed as the companion video.

Accuracy falls from 98\% to 55\% (Table 1). The last row shows why: with 64 features the farthest training image is typically 3.9 times as far as the nearest, but with 1{,}024 features only 1.13 times. Figure 4b shows it for one test image, a 3: with 64 features its distances to the training images spread from 1 to 4 times the nearest one; with 1{,}024 features they are all crowded between 1 and 1.13.

(a) One test digit, a 3, with 64, 256, 576 and 1{,}024 features. (b) Its distances to all 1{,}257 training images, divided by the distance to the nearest one. (c) Test accuracy of 1-nearest neighbor (solid, left axis) and the median ratio of the farthest to the nearest distance (dashed, right axis). Computed for this tutorial, with the same data and seed as the companion video.
Figure 4. (a) One test digit, a 3, with 64, 256, 576 and 1{,}024 features. (b) Its distances to all 1{,}257 training images, divided by the distance to the nearest one. (c) Test accuracy of 1-nearest neighbor (solid, left axis) and the median ratio of the farthest to the nearest distance (dashed, right axis). Computed for this tutorial, with the same data and seed as the companion video.
Try itDrown a digit in noise features

Choose how many features each image has. The handwritten 3 of Figure 4 sits in the middle of a canvas of random pixels. Right: its real distances to all 1,257 training digits, divided by the distance to the nearest one; the clean 64-feature histogram stays in gray for comparison. When the bars crowd at 1, the nearest neighbor is barely nearer than any other.

Worked example 2 (Why the noise swamps the signal). Follow the test 3 of Figure 4. With 64 features its squared distance to the nearest training image (also a 3) is 258, and to a typical training image 2{,}088 (the median): the signal is a gap of 1{,}830. On the 32\times32 canvas there are 960 noise pixels. For two independent uniform values on [0,16], \E(u-v)^2=16^2/6=42.67, so the noise adds about 960\times42.67=40{,}960 to every squared distance, with a standard deviation of \sqrt{960\cdot16^4(1/15-1/36)}=1{,}564 that differs from one training image to the next. The random part, 1{,}564, is as large as the whole signal, 1{,}830. The ranking of the training images is reshuffled: the nearest neighbor with 1{,}024 features is a 6, and the true nearest 3 falls to rank 330. All the distances, meanwhile, are about \sqrt{40{,}960+\text{signal}}: the nearest at \sqrt{39{,}425} and the median at \sqrt{45{,}237}, a ratio of only 1.07.

5.2Distances concentrate

The example is an instance of a general law. Take two points x,y uniform in [0,1]^d. The squared distance D^2=\sum_{j=1}^{d}\;(x_j-y_j)^2 is a sum of d independent terms. For one coordinate, the difference x_j-y_j has the triangular density 1-|t| on [-1,1], which gives

\E(x_j-y_j)^2=\tfrac16,\qquad \E(x_j-y_j)^4=\tfrac1{15},\qquad \Var\bigl((x_j-y_j)^2\bigr)=\tfrac1{15}-\tfrac1{36}=\tfrac7{180}. \tag{6}

Proposition 1 (Relative spread of distances). For x,y independent and uniform in [0,1]^d, \E D^2=d/6 and \Var D^2=7d/180. For large d,

\mu_D\approx\sqrt{d/6},\qquad \sigma_D\approx\sqrt{7/120}=0.24,\qquad \frac{\sigma_D}{\mu_D}\approx\frac{0.59}{\sqrt d}. \tag{7}

Means and variances of independent terms add, which gives the first two statements from (6). By the law of large numbers D^2 is close to its mean, and for a small change D=\sqrt{D^2} changes by \Delta(D^2)/(2D), so \sigma_D\approx\sigma_{D^2}/(2\mu_D)=\sqrt{7d/180}\big/\bigl(2\sqrt{d/6}\bigr)=\sqrt{7/120}. Dividing by \mu_D=\sqrt{d/6} gives \sqrt{7/20}/\sqrt d=0.59/\sqrt d.

The typical distance grows like \sqrt d, but its spread stays fixed at about 0.24. Relative to the distance itself, all distances become the same. With 500 uniform points, the relative spread of the pairwise distances was 0.476 for d=2, 0.193 for d=10, 0.060 for d=100 and 0.019 for d=1{,}000, on the line 0.59/\sqrt d (Figure 5). From one query point, the farthest of the other points was 26 times as far as the nearest in two dimensions, 3.9 times in ten, 1.38 times in a hundred and 1.11 times in a thousand.

Distance concentration for 500 points uniform in [0,1]^d. (a) Histograms of the pairwise distances divided by their mean: the hump narrows into a spike at 1. (b) The relative spread (standard deviation over mean) against d, with the prediction 0.59/\sqrt d of Proposition 1. Computed for this tutorial, with the same seed as the companion video.
Figure 5. Distance concentration for 500 points uniform in [0,1]^d. (a) Histograms of the pairwise distances divided by their mean: the hump narrows into a spike at 1. (b) The relative spread (standard deviation over mean) against d, with the prediction 0.59/\sqrt d of Proposition 1. Computed for this tutorial, with the same seed as the companion video.

Theorem 1 (Beyer et al., 1999). Let D_d be the distance from a query point to a random data point in dimension d. If \Var\bigl(D_d/\E D_d\bigr)\to0 as d\to\infty, then for any fixed number of data points and every \epsilon>0,

P\bigl(D_{\max}\le(1+\epsilon)\,D_{\min}\bigr)\to1.

The condition holds for independent, identically distributed features, as Proposition 1 shows, and for much broader families; Beyer et al. (1999) saw the effect already at 10 to 15 dimensions. It does not hold when the data are clustered or have a low intrinsic dimension, which is the escape route of Section 6.

5.3Other distances, and peaking

Other norms. Aggarwal et al. (2001) showed that for the L_k norm the absolute gap D_{\max}-D_{\min} grows like d^{1/k-1/2}: it grows for L_1 (Manhattan), stays bounded for L_2, and shrinks for k\ge3. The relative contrast falls for every k, but lower k keeps more of it, so L_1 often beats Euclidean distance on high-dimensional data.

Cosine similarity. For two random Gaussian vectors in \mathbb{R}^d the cosine of their angle has mean 0 and standard deviation 1/\sqrt d. In simulation it was 0.71, 0.32, 0.10 and 0.032 for d=2, 10, 100 and 1{,}000, and at d=1{,}000 99.8\% of the pairs had a cosine between -0.1 and 0.1: random high-dimensional vectors are almost orthogonal. Cosine removes the effect of vector length but concentrates just the same.

Peaking. Hughes (1968) described what the digits experiment shows in its extreme form: with a fixed number of training examples, adding features first helps, because each carries some information, and then hurts, because the number of parameters or the volume to cover outgrows the data. The best number of features grows with the size of the training set. Stone (1982) made the cost precise for nonparametric regression: for a function with s smooth derivatives the best possible mean squared error falls like N^{-2s/(2s+d)}. With s=2 that is N^{-0.8} for d=1 but N^{-0.29} for d=10: halving the error needs 2.4 times more data in one dimension, 11 times more in ten, and 6.7\times10^{7} times more in a hundred.

Pause and think. In Example 2, the noise added the same amount, about 40{,}960, to every squared distance on average. Why did that not leave the ranking of neighbors unchanged?

Show the answer

A constant shift would not change the ranking; the problem is the random part. Each training image has its own noise pixels, so the shift varies with standard deviation 1{,}564, as large as the signal gap of 1{,}830. The fluctuation grows like \sqrt{m} with the number of noise pixels m, while the signal stays fixed.