Contents
02Training accuracy lies
2.1The data and the model
Each of the 569 patients had a fine-needle aspirate of a breast lump; Street et al. (1993) measured the cell nuclei in the images, 30 numbers per patient, and 212 tumors were malignant and 357 benign. We use two measurements so that every model is a picture: the mean radius of the nuclei and their mean texture (the variation of gray levels). Both are standardized to mean 0 and variance 1, with the means and variances computed from the training patients of each fit.
The classifier is k-nearest neighbors: to label a new tumor, find the k training tumors closest to it and take a majority vote. Training stores the patients; the parameters are the stored data. The number of neighbors k is the hyperparameter: it is fixed before training and controls how smooth the decision boundary is.
2.2A perfect score that means nothing
Hide 20\% of the patients and train 1-nearest neighbor on the other 455. On those 455 patients its accuracy is 100\% (Figure 1a). The reason is not skill: the nearest training tumor to a training tumor is the tumor itself, at distance zero, so the model always returns its own label. The orange islands inside the blue region are single malignant tumors the model has memorized. On the 114 hidden patients, the same model is right only 87.7\% of the time: 14 mistakes (Figure 1b). With 15 neighbors the training accuracy drops to 87.7\%, yet the accuracy on the hidden patients rises to 92.1\% (Figure 1c). The model that looked worse on the training data is the better one.
This is overfitting, as in Episode 2: a flexible model fits the noise of its training data, has low bias and high variance, and generalizes worse. Training accuracy measures memory. It is like grading students with the exact questions they practiced on, answer key included.
2.3Why training error cannot choose a hyperparameter
Suppose we chose k by training accuracy. On the 455 training patients of Chapters 4 and 5, k=1 scores 100\%, k=3 scores 93.2\% and k=15 scores 89.0\%: the choice would always be k=1. Murphy (2022, §5.4.2) makes the general point for a regularized objective R(\theta,\D)+\lambda C(\theta) with a complexity penalty C\ge0: choosing
always gives \hat\lambda=0, because the penalty can only add to the training loss. The training loss underestimates the loss on new data, and it underestimates it most for the most flexible model. This gap is called the optimism of the training error, and for squared error it has a simple exact form.
Proposition 1 (Optimism of training error, Hastie et al., 2009, §7.4). Let y_i=f(x_i)+\varepsilon_i with fixed inputs x_i and independent noise of mean 0 and variance \sigma^2, and let \hat y_i be the fitted values of any method. Let y_i^0 be a new response at the same input x_i. Then
Write f_i=f(x_i). The new response is independent of \hat y_i, so \E(y_i^0-\hat y_i)^2=\sigma^2+\E(f_i-\hat y_i)^2. For the training response, y_i-\hat y_i=\varepsilon_i+(f_i-\hat y_i), so \E(y_i-\hat y_i)^2=\sigma^2+\E(f_i-\hat y_i)^2+2\E\bigl[\varepsilon_i(f_i-\hat y_i)\bigr]. Since f_i is a constant and \E\varepsilon_i=0, \E[\varepsilon_i(f_i-\hat y_i)]=-\E[\varepsilon_i\hat y_i]=-\Cov(y_i,\hat y_i). Subtracting the two expectations and averaging over i gives (1).
The more a fitted value depends on its own response, the more optimistic the training error. For k-nearest-neighbor regression, \hat y_i=\frac1k\sum_{j\in N_k(x_i)}y_j, and a training point is always one of its own k neighbors. The neighborhoods depend only on the inputs, so with independent noise
For k=1 the training error is exactly 0 while the error on new responses is 2\sigma^2. The last quantity in (2) is the effective number of parameters of k-nearest neighbors, N/k (Hastie et al., 2009, §2.3.2): if the neighborhoods did not overlap there would be N/k of them, each with its own mean. With 455 training patients, k=1 has 455 effective parameters, one per patient, while k=41 has about 11.
Pause and think. A decision tree grown until every leaf holds one class also scores 100\% on its training data. If you chose the depth of the tree by training accuracy, what would you choose?
Show the answer
The deepest tree, always, for the same reason that training accuracy picks k=1: each extra split can only fit the training data better. Depth, like k, must be chosen on data the tree has not seen.