Curse of Dimensionality for a Nearest Neighbor Classifier
In high dimensions the nearest neighbors of a point are not near, because the same observations leave the space mostly empty: the curse of dimensionality. James, Witten, Hastie, and Tibshirani describe it in Section 3.5 of An Introduction to Statistical Learning, after a nearest-neighbor fit loses to linear regression as features are added. The decline "is a common problem for KNN, and results from the fact that in higher dimensions there is effectively a reduction in sample size." With 50 training observations, "spreading 50 observations over p = 20 dimensions results in a phenomenon in which a given observation has no nearby neighbors—this is the so-called curse of dimensionality." That is, "the K observations that are nearest to a given test observation x0 may be very far away from x0 in p-dimensional space when p is large, leading to a very poor prediction of f(x0) and hence a poor KNN fit."
In Section 6.4.3 they add a second face of the same idea. The test error "tends to increase as the dimensionality of the problem (i.e. the number of features or predictors) increases, unless the additional features are truly associated with the response." And: "In general, adding additional signal features that are truly associated with the response will improve the fitted model, in the sense of leading to a reduction in test set error. However, adding noise features that are not truly associated with the response will lead to a deterioration in the fitted model, and consequently an increased test set error."
Try it by hand. Suppose each feature is uniform on 0 to 1 and you have 1,000 observations. Cut every feature into 10 bins. With 1 feature there are 10 cells, so each holds 100 observations on average. With 2 features there are 10² = 100 cells, 10 observations each. With 10 features there are 10¹⁰ cells, and 1,000 observations give 10⁻⁷ per cell, so almost every cell is empty. With 100 features the count of cells is 10¹⁰⁰. Now the idea behind the book's Exercise 4 (pp. 189 to 190), with the numbers my own. To catch 10% of the observations in a hypercube around a test point, each side must have length 0.1^(1/p): 0.1 for p = 1, 0.316 for p = 2, 0.794 for p = 10, and 0.977 for p = 100. A neighborhood that spans almost the whole range of every feature is not local. If instead you keep only observations within 10% of the range on every feature, the fraction you keep is 0.1^p: 0.1, 0.01, and 10⁻¹⁰⁰ for p = 1, 2, and 100. A simulation shows the same thing in distances. With 1,000 uniform points and 300 random test points, the average distance to the nearest neighbor was 0.0005 for p = 1, 0.016 for p = 2, 0.51 for p = 10, and 3.33 for p = 100. The average distance to a random point was 0.33, 0.51, 1.27, and 4.07. The ratio of nearest to average distance climbs from 0.001 to 0.031, 0.40, and 0.82, so in 100 dimensions the nearest point is hardly nearer than a random one. The data are random points I generated, and the code ran with NumPy.
The trap: concluding that every high-dimensional problem is hopeless, or that more features always help. The book says neither: extra features help when they carry signal and hurt when they are noise. A caution of my own, not from the sections I read: this toy uses points spread evenly through the whole space. Real data often lie near a lower-dimensional surface, where neighbors can stay close, and a model that does not rely on local neighbors, such as the book's linear regression example, is hurt less. The numbers show a mechanism, not a rule for how many observations any problem needs.
Prof Rod's rule: before adding features to a method that relies on nearby points, ask how many observations you would need to cover the space, and add only features you have reason to think carry signal.