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 06 of 09

06Don’t let the test leak

6.1A famous trap

All of this works only if the held-out patients stay truly unseen, in every step. Hastie et al. (2009, §7.10.2) describe a trap that has caught many published studies. Take N=50 patients in two classes of 25 and p=5{,}000 features of pure noise, independent of the labels; our version uses standard normal features and coin-flip labels. There is nothing to learn, so the honest error of any classifier is 50\%. Now:

  • The wrong way. First pick the 100 features with the largest correlation with the labels, using all 50 patients. Then cross-validate a 1-nearest-neighbor classifier on those 100 features. Over 50 simulations the average cross-validation error was 1.0\%, and exactly 0\% in 31 of them; Hastie et al. (2009) report 3\% for their simulations.
  • The right way. Inside each fold, pick the 100 features again, from the training patients of that fold only, and cross-validate the whole procedure. The average error was 49.6\% (between 26\% and 70\% in single simulations): the truth.
Cross-validation the wrong and the right way, after Hastie et al. (2009, §7.10.2). (a) Absolute correlations of 5{,}000 noise features with random labels, over all 50 patients, with the distribution expected by chance alone; orange: the 100 chosen. (b) Correlation of the chosen features with the labels on the left-out patients of each fold, with the sign it had when the feature was chosen (50 simulations, 5 folds). (c) Cross-validation error in 50 simulations. Computed for this tutorial, with the same setup and seeds as the companion video.
Figure 6. Cross-validation the wrong and the right way, after Hastie et al. (2009, §7.10.2). (a) Absolute correlations of 5{,}000 noise features with random labels, over all 50 patients, with the distribution expected by chance alone; orange: the 100 chosen. (b) Correlation of the chosen features with the labels on the left-out patients of each fold, with the sign it had when the feature was chosen (50 simulations, 5 folds). (c) Cross-validation error in 50 simulations. Computed for this tutorial, with the same setup and seeds as the companion video.
Try itCross-validate the wrong way and the right way

Fifty patients, 5,000 noise features and coin-flip labels: the honest error is 50%. Choose the way and step through the 50 simulations of Figure 6. Left: on the 10 patients left out in the first fold, the correlation of each of the 100 chosen features with the labels, signed as when the feature was chosen (gray: the other way). Right: the cross-validation error of every simulation.

6.2Where exactly is the data leaked?

When we chose the 100 features from all 5{,}000, we had already used the data of the patients in the validation folds. Look at the numbers. For two independent variables, the correlation over n=50 patients is approximately normal with mean 0 and standard deviation 1/\sqrt{n-1}=1/7=0.143. Among 5{,}000 noise features, half have |r| above 0.6745/7=0.096 (we found a median of 0.094), and the top 100, the top 2\%, have |r| above about 2.326/7=0.33 (we found 0.324, and a maximum of 0.491; Figure 6a). These large correlations are luck, but the luck includes the patients who will later sit in the validation folds. On those left-out patients the chosen features still lean the same way, with an average correlation of 0.38 in the wrong way and 0.00 in the right way (Figure 6b; Hastie et al., 2009, §7.10.2 show the same effect). One-nearest neighbor on 100 features that all lean toward the validation labels classifies them almost perfectly.

6.3The rule: everything that learns from data goes inside the folds

Cross-validation estimates the error of a procedure, so it must repeat the whole procedure in every fold, using only that fold’s training part. The samples must be left out before any step that looks at the labels (Hastie et al., 2009, §7.10.2). This covers:

  • feature selection and screening by correlation with the labels (the trap above);
  • hyperparameter tuning (Chapter 5; nested cross-validation when you also want an honest estimate);
  • oversampling or reweighting of rare classes, and target encoding of categorical features;
  • scaling, imputation of missing values and principal component analysis, which learn means, variances or directions from the data.

Hastie et al. (2009, §7.10.2) allow one exception: an unsupervised screening that ignores the labels, such as keeping the features with the highest variance across all patients, gives no feature an unfair advantage and may be done first. Still, the safe habit is to put every step in one pipeline that is refit inside each fold; in this tutorial even the standardization of the two measurements was refit on the training part of every fold.

6.4Groups and time

Two kinds of data break the assumption that the examples are independent, and with it the random split.

  • Several rows per patient (several scans, visits or images). If one patient’s rows fall in both the training and the validation folds, the model can recognize the patient instead of learning the disease. Assign whole patients to folds (group K-fold).
  • Time series. A random split lets the model train on the future and validate on the past. Train on the past and validate on the next block, then move forward (a rolling or expanding window), without shuffling. Bergmeir et al. (2018) show that ordinary K-fold cross-validation remains valid for purely autoregressive models whose errors are uncorrelated, but forward validation is the safe default.

6.5A real case

Ambroise and McLachlan (2002) re-examined microarray studies that reported cancer classifiers with almost no error, built from a handful of genes chosen among thousands. The genes had been chosen using all the tissue samples, and the error estimated afterwards. On the well-known colon cancer and leukemia data sets they repeated the gene selection inside every training fold, and the estimated error rose substantially: the near-perfect accuracy was largely selection bias, exactly the wrong way of Figure 6.

Pause and think. Which of these leak information from the validation folds: (i) standardizing all 569 patients before splitting; (ii) filling missing values with the mean of all patients; (iii) removing outliers by their distance from the overall mean; (iv) keeping the 1{,}000 features with the highest variance; (v) keeping the features with the smallest p-value in a test against the labels?

Show the answer

All five use the validation patients, but not equally. (v) uses the labels and can create the dramatic bias of Figure 6. (i)–(iv) use only the inputs; (iv) is the unsupervised screening that Hastie et al. (2009) allow, and (i)–(iii) usually cause small leaks. The safe answer for all five is the same: do them inside each fold, in a pipeline.