MoonSay /Tutorials/Machine Learning Interview #11
Contents
  1. 01Introduction
  2. 02Training accuracy lies
  3. 03One mock exam
  4. 04Everyone takes a turn
  5. 05Tune, then refit
  6. 06Don’t let the test leak
  7. 07Common misconceptions
  8. 08Interview questions and model answers
  9. 09Summary
  10. References
Cross-Validation/Chapter 04 of 09

04Everyone takes a turn: K-fold cross-validation

4.1The procedure

So we switch from one split to many, and let every patient take a turn as judge. First, lock away a test set: 114 patients, stratified so that it has the same share of malignant tumors as the whole, who will never touch training or tuning. Cross-validation then works on the 455 training patients alone. Cut them at random into K=5 folds of 91. Train on four folds and validate on the fifth; rotate, so that each fold is the validation set once (Figure 3a). That is five trainings and five validation scores, and every training patient is validated exactly once. The cross-validation estimate is the average (Murphy, 2022, eq. 4.106):

\mathrm{CV}(\lambda)=\frac1K\sum_{j=1}^{K}\;R\bigl(\hat\theta_\lambda(\D_{-j}),\,\D_j\bigr), \tag{4}

the loss on fold j of the model trained without fold j. For accuracy, replace the loss by the accuracy. When the folds have different sizes n_j, weight each fold by n_j/N; then \mathrm{CV} is exactly the average, over all N training points, of the loss of the one model that did not see that point.

(a) Five-fold cross-validation of 15-nearest neighbors on the 455 training patients; the 114 test patients stay locked. The right column is the accuracy on the validation fold. (b) The estimate repeated with different random partitions of the same training patients: 200 single validation splits of 91 patients, 200 reshuffled five-fold runs and 50 reshuffled ten-fold runs; the dotted line is leave-one-out. Computed for this tutorial, with the same data and seeds as the companion video.
Figure 3. (a) Five-fold cross-validation of 15-nearest neighbors on the 455 training patients; the 114 test patients stay locked. The right column is the accuracy on the validation fold. (b) The estimate repeated with different random partitions of the same training patients: 200 single validation splits of 91 patients, 200 reshuffled five-fold runs and 50 reshuffled ten-fold runs; the dotted line is leave-one-out. Computed for this tutorial, with the same data and seeds as the companion video.
Try itEveryone takes a turn

The 455 training patients of Worked example 2, cut into five folds of 91 (one row per fold, one cell per patient); the 114 test patients stay locked. Each training uses four folds and validates the fifth. Blue cells were classified correctly by the model that did not see them, orange cells wrongly. Right: the five validation scores and their running average, the cross-validation estimate of equation (4).

Worked example 2 (Five folds by hand). For k=15, the five validation folds are classified with 11, 5, 9, 12 and 14 mistakes out of 91: accuracies 87.9\%, 94.5\%, 90.1\%, 86.8\% and 84.6\%. Their average is 88.8\%. Since the folds are equal, this is also (455-51)/455=404/455: of the 455 training patients, 404 were classified correctly by a model that had not seen them. The five fold scores themselves range over ten points, the same luck as in Chapter 3; averaging them is what makes the estimate useful. Each of the five fold models was trained on 364 patients and is then thrown away: their only job was to produce the scores.

4.2Why five folds are about seven times steadier

Repeat the whole procedure with a different random partition of the same 455 patients, 200 times. The five-fold estimate stays between 87.7\% and 89.9\%, with a standard deviation of 0.44 points. A single validation split of 91 patients from the same 455, repeated 200 times, ranges from 81.3\% to 95.6\% with a standard deviation of 2.93 points, close to the 3.02 points of (3) with n=91 and N=455. Cross-validation is 6.7 times steadier (Figure 3b).

The reason is more than averaging five numbers, which alone would gain at most a factor \sqrt5=2.2. In (3), the wobble comes from which patients are judged. Cross-validation judges all 455 every time, so that source of luck disappears completely. What remains is that each patient is classified by a slightly different model in each partition, and only borderline patients near the boundary ever change their verdict.

4.3How many folds?

Three things change with K (Table 1).

  • Training size. Each fold model trains on (K-1)N/K points: 227 of 455 for K=2, 364 for K=5, 409 for K=10 and 454 for leave-one-out. A model trained on less data is usually a little worse, so with small K the estimate is pessimistic when the learning curve is still rising (Hastie et al., 2009, §7.10.1).
  • Variability. More folds use more of the data for training and average more scores; the reshuffle standard deviation fell from 0.44 points for K=5 to 0.33 for K=10, and leave-one-out has none. But the fold models of leave-one-out are trained on almost identical data, so their errors are highly correlated, and its estimate can vary a lot from one data set to another (Hastie et al., 2009; James et al., 2013, §7.10.1).
  • Cost. K trainings per candidate.

The usual choice is K=5 or 10 (Hastie et al., 2009, §7.10.1). Kohavi (1995) recommended stratified ten-fold cross-validation, in which every fold keeps the class proportions of the whole; for our training patients (170 malignant of 455) that means about 34 malignant tumors per fold.

methodtrains ontrainings per candidatemean estimateSD (points)
one validation split, 80/20364188.3\%2.93
5-fold cross-validation364588.9\%0.44
10-fold cross-validation4091088.8\%0.33
leave-one-out45445588.8\%0
Table 1. Estimating the accuracy of 15-nearest neighbors from the 455 training patients. “SD” is the standard deviation of the estimate, in percentage points, when the random partition is redrawn (200, 200 and 50 times). Computed for this tutorial, with the same data and seeds as the companion video.

4.4Leave-one-out, and a shortcut for least squares

With K=N each fold is a single point: train on N-1 points, predict the one left out, repeat N times. For least squares, the N fits are unnecessary.

Proposition 3 (Leave-one-out for least squares, Allen, 1974; Hastie et al., 2009, §7.10.1). Let X be the N\times p input matrix (with a column of ones for the intercept), \hat y=Hy the least-squares fit with H=X(X^\top X)^{-1}X^\top, and h_{ii}=x_i^\top(X^\top X)^{-1}x_i the diagonal of H. If \hat y_i^{(-i)} is the prediction at x_i of the fit without point i, then

y_i-\hat y_i^{(-i)}=\frac{y_i-\hat y_i}{1-h_{ii}},\qquad \mathrm{CV}_{(N)}=\frac1N\sum_{i=1}^{N}\;\Bigl(\frac{y_i-\hat y_i}{1-h_{ii}}\Bigr)^2. \tag{5}

Let A=X^\top X. Removing point i gives A_{-i}=A-x_ix_i^\top, and the Sherman–Morrison formula gives

A_{-i}^{-1}=A^{-1}+\frac{A^{-1}x_ix_i^\top A^{-1}}{1-h_{ii}},\qquad\text{hence}\qquad x_i^\top A_{-i}^{-1}=x_i^\top A^{-1}\Bigl(1+\frac{h_{ii}}{1-h_{ii}}\Bigr)=\frac{x_i^\top A^{-1}}{1-h_{ii}}.

The fit without point i is \hat\beta_{-i}=A_{-i}^{-1}(X^\top y-x_iy_i), so

\hat y_i^{(-i)}=x_i^\top\hat\beta_{-i}=\frac{x_i^\top A^{-1}X^\top y-h_{ii}y_i}{1-h_{ii}}=\frac{\hat y_i-h_{ii}y_i}{1-h_{ii}}.

Subtracting from y_i,

y_i-\hat y_i^{(-i)}=\frac{y_i(1-h_{ii})-\hat y_i+h_{ii}y_i}{1-h_{ii}}=\frac{y_i-\hat y_i}{1-h_{ii}}.\qedhere

The leverage h_{ii} measures how strongly point i pulls the fit toward itself; dividing by 1-h_{ii} undoes that pull. As a check, regress the worst radius of the 569 tumors on the mean radius and mean texture (p=3 coefficients). The training mean squared error is 1.360; the shortcut (5) gives a leave-one-out error of 1.385, and 569 separate fits give the same number to fourteen decimal places. The leverages lie between 0.0018 and 0.0404 and sum to \tr H=p=3. Replacing every h_{ii} by their average p/N gives generalized cross-validation, which only needs the trace (Hastie et al., 2009, §7.10.1).

Pause and think. A classifier always predicts the most common class in its training data. A data set has 50 examples of each of three classes, so the classifier is right a third of the time on new data. What does leave-one-out report?

Show the answer

0\% accuracy. When an example of class A is left out, the training data hold 49 of class A and 50 each of B and C, so the prediction is B or C, never A. Every left-out example is misclassified. This is the example of Kohavi (1995): leave-one-out can fail badly when removing one point changes the model in a systematic way.