Contents
05Tune, then refit
5.1The full recipe
Cross-validation’s main job is choice: picking a hyperparameter. Following Murphy (2022, §4.5.4–4.5.5) and Hastie et al. (2009, §7.2, §7.10):
- Lock away a test set. It is used once, at the very end.
- Choose M candidate settings \lambda_1,\dots,\lambda_M (a grid).
- Cut the training data into K folds, once. Every candidate uses the same folds, so the comparison is fair: they share the luck of the partition, as the two candidates of Figure 2b shared the luck of each split.
- For every candidate and every fold, train with that setting on the other K-1 folds and score on the fold; average the K scores to get \mathrm{CV}(\lambda_m) by (4).
- Choose \hat\lambda=\operatorname{argmax}_m\mathrm{CV}(\lambda_m) for accuracy (\operatorname{argmin} for a loss).
- Refit once with \hat\lambda on all the training data, \hat\theta=\hat\theta_{\hat\lambda}(\D) (Murphy, 2022, eq. 4.105). The MK fold models are thrown away.
- Open the test set once and report that accuracy.
The number of trainings is
Murphy (2022, §5.4.4) names the drawback plainly: cross-validation is slow, because the model is fitted many times.
5.2Ten candidates for k
We try M=10 values, k=1, 3, 7, 15, 25, 41, 61, 101, 201 and 301, on the same five folds as in Example 2: 10\times5=50 trainings (Table 2). With one neighbor the model memorizes and scores 86.4\%. With 301 neighbors the model averages the pattern away. Each training part of four folds holds between 133 and 140 malignant tumors, fewer than half of 301, so a malignant majority is impossible: every fold model calls every tumor benign, and the score, 62.6\%, is just the share of benign tumors among the training patients (285 of 455). The best average is k=41, at 90.1\% (Figure 4). Accuracy against k is the bias–variance tradeoff of Episode 2 turned upside down: variance dominates on the left, bias on the right. Figure 5 shows the three regimes as decision regions.
| k | N/k | fold 1 | fold 2 | fold 3 | fold 4 | fold 5 | \mathrm{CV}(k) | SE |
|---|---|---|---|---|---|---|---|---|
| 1 | 455 | 86.8 | 89.0 | 83.5 | 85.7 | 86.8 | 86.4 | 0.9 |
| 3 | 152 | 86.8 | 91.2 | 86.8 | 87.9 | 84.6 | 87.5 | 1.1 |
| 7 | 65 | 91.2 | 93.4 | 89.0 | 84.6 | 84.6 | 88.6 | 1.8 |
| 15 | 30 | 87.9 | 94.5 | 90.1 | 86.8 | 84.6 | 88.8 | 1.7 |
| 25 | 18 | 89.0 | 94.5 | 91.2 | 87.9 | 86.8 | 89.9 | 1.4 |
| \mathbf{41} | 11 | 89.0 | 93.4 | 90.1 | 90.1 | 87.9 | \mathbf{90.1} | 0.9 |
| 61 | 7.5 | 87.9 | 93.4 | 92.3 | 87.9 | 87.9 | 89.9 | 1.2 |
| 101 | 4.5 | 87.9 | 90.1 | 90.1 | 86.8 | 87.9 | 88.6 | 0.7 |
| 201 | 2.3 | 87.9 | 87.9 | 84.6 | 86.8 | 82.4 | 85.9 | 1.1 |
| 301 | 1.5 | 64.8 | 67.0 | 62.6 | 59.3 | 59.3 | 62.6 | 1.5 |
5.3Refit once, test once
We choose k=41 and train one last time, with 41 neighbors, on all 455 training patients: 50+1=51 trainings in all. Only now do we open the locked test set, exactly once: the refit model classifies 93.0\% of the 114 test patients correctly. That is the number we report.
Two remarks. First, why refit? Each fold model saw only 364 patients. Cross-validation rated the recipe “41-nearest neighbors trained on patients like these”, not any one fitted model, and the refit model applies the recipe to all the data we have. Second, why is the test accuracy, 93.0\%, higher than the cross-validation estimate, 90.1\%? Partly because the refit model trained on more patients, and mostly luck: by (3) a test set of 114 carries a standard deviation of about \sqrt{0.93\times0.07/114}=2.4 points, so a difference of three points is unremarkable. Here the locked test set would also have chosen k=41 (Figure 4), but it must not be asked: a test set that helps choose is no longer a test.
5.4The one-standard-error rule
The cross-validation curve near its peak is flat compared with its error bars: k=25, 41 and 61 are within a quarter of a point of each other. The one-standard-error rule picks the simplest model whose score is within one standard error of the best (Hastie et al., 2009; Murphy, 2022, §7.10.1). For k-nearest neighbors, simpler means more neighbors (fewer effective parameters, N/k). The best score is 90.1\% with a standard error of 0.92 points, so the threshold is 89.2\%; the largest k above it is k=61, at 89.9\%. Murphy (2022, §4.5.5.1) computes the standard error from the individual losses instead, \sqrt{0.901\times0.099/455}=1.40 points; the rule chooses k=61 either way. Refit on all 455 patients, k=61 scores 91.2\% on the test set. The rule trades a little estimated accuracy for a smoother, more stable model; on this test set it happened to cost 1.8 points, which is within the test set’s luck.
5.5Is the best cross-validation score an honest estimate?
Not quite. \mathrm{CV}(\hat\lambda) is the maximum of M noisy numbers, and the maximum of noisy numbers is biased upward: we pick the candidate that was best and lucky. The bias grows with the number of candidates and shrinks with the size of the data; with many candidates and few examples it can be as large as the differences between learning methods (Varma and Simon, 2006; Cawley and Talbot, 2010). Stone’s two jobs, choice and assessment, should not use the same scores. Two remedies:
- A locked test set, as above, used once.
Nested cross-validation (Varma and Simon, 2006), when the data are too few to lock any away. An outer loop of K_{\text{out}} folds plays the role of the test set. On each outer training part, run the whole recipe, inner cross-validation with K_{\text{in}} folds, choice and refit, then score the result once on the outer fold. The average of the outer scores estimates how well the whole procedure, tuning included, performs on new data. The cost is
\text{trainings}=K_{\text{out}}\,\bigl(M\cdot K_{\text{in}}+1\bigr). \tag{7}
Worked example 3 (Nested cross-validation on all 569 patients). With five outer folds of 114 patients (one of 113) and the same ten candidates and five inner folds, the inner loops chose k=41, 41, 41, 15 and 41, with best inner scores averaging 90.3\%. The five outer scores were 87.7\%, 93.0\%, 86.0\%, 89.5\% and 93.8\%, an estimate of 90.0\% for the procedure “choose k by five-fold cross-validation, then refit”. That is 5\times(10\times5+1)=255 trainings. Here the optimism of the best inner score is only 0.3 points, because ten candidates on a smooth curve leave little room to get lucky. Note that the outer folds did not all choose the same k: nested cross-validation assesses a procedure, not one setting.
Pause and think. A colleague tries 500 hyperparameter combinations with five-fold cross-validation on 200 examples and reports the best cross-validation accuracy as the expected accuracy in production. What would you say?
Show the answer
The search cost 500\times5+1=2{,}501 trainings, and the reported number is the best of 500 noisy estimates from 200 examples, so it is optimistic, possibly by a lot. Report the score on a locked test set, or the outer score of nested cross-validation, which would cost K_{\text{out}}\times2{,}501 trainings.