Contents
05Interview follow-ups
5.1What does a prediction cost?
Training is free, but each prediction measures the distance to all n training points, each with d measurements: order n\times d operations, plus sorting out the k smallest; and the whole training set, n\times d numbers, stays in memory. For one wine that is 124 distances over 13 measurements; for millions of points and many predictions it is the bottleneck. This is the opposite of a parametric model such as logistic regression, which is slow to train and fast to predict.
Tree indexes speed up the search. Jon Bentley’s k-d tree (Bentley, 1975) splits the training points in half on one coordinate, then each half on the next coordinate, and so on, into nested boxes. A search descends to the box that contains x, then visits a neighboring box only if it could contain a point closer than the k-th best found so far. In low dimensions this takes logarithmic expected time (Friedman et al., 1977). Above about ten dimensions few boxes can be skipped, and practical systems use approximate nearest-neighbor search instead (Murphy, 2022, §16.1.3). Standardize before building the index, since the index uses the same distance.
5.2Does it work in high dimensions?
Poorly, as in Episode 10. Suppose the inputs are spread uniformly in a D-dimensional unit cube and we want a neighborhood that holds a fraction p of the data. A cube with that volume has edge length p^{1/D} (Hastie et al., 2009, §2.5). In D=10 dimensions, 10\% of the data needs 0.79 of the range of every axis (Figure 5a), and even 1\% needs 0.63 (Murphy, 2022, §16.1.2). The “nearest” neighbors are no longer near, so they say little about x. A related effect: as the dimension grows, the distances from a point to its nearest and to its farthest neighbor become almost equal, so “nearest” loses its meaning (Beyer et al., 1999). The remedies are fewer, better measurements (feature selection, principal component analysis) or a learned distance (Murphy, 2022, §16.2).
5.3How good can it be?
The lowest error any classifier can reach is the Bayes error R^*: the error of the rule that knows the true class probabilities P(c\mid x) and always picks the most likely class. No classifier can do better. Cover and Hart proved that one neighbor, with unlimited data, is surprisingly close to it.
Theorem 1 (Cover and Hart, 1967). With C classes, as the number of training points grows without limit, the error R of the one-nearest-neighbor rule satisfies
Let \eta(x)=P(y=1\mid x). At x the Bayes rule picks the more likely class and errs with probability r^*(x)=\min\{\eta(x),1-\eta(x)\}; the Bayes error is its average, R^*=\mathbb E[r^*(x)]. With unlimited data the nearest neighbor x' is arbitrarily close to x, so its label is an independent draw with the same probability \eta(x). The rule errs when the two labels differ:
because \eta(1-\eta) is the same whichever of \eta and 1-\eta is the smaller. Average over x:
since \mathbb E[{r^*}^2]\ge(\mathbb E[r^*])^2 (the variance of r^* is not negative). With C=2 this is the upper bound in (12). The lower bound holds because no rule beats the Bayes rule.
In words, half of what unlimited data can tell you about a point’s class is already in its single nearest neighbor (Cover and Hart, 1967): when the best possible error is small, one neighbor at most doubles it (Figure 5b). The catch is “unlimited data”. How fast the error approaches the bound depends on how densely the data fill the space, which is exactly what fails in high dimensions (Murphy, 2022, §16.1).