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
Machine Learning Interview #11 · Tutorial

Cross-ValidationWhy Training Accuracy Lies

How to choose hyperparameters honestly, and keep the test set truly unseen

A tutorial companion to the video Machine Learning Interview #11: Cross-Validation: Why Training Accuracy Lies PDF

01Introduction

1.1The question

“Why do we use cross-validation, and how does it work?” Every machine learning model has settings that training does not learn: the number of neighbors k in k-nearest neighbors, the strength \lambda of a regularizer, the depth of a tree, the learning rate of a neural network. These are hyperparameters. Training learns the parameters for one fixed setting; something else must choose the setting. The obvious judge, accuracy on the training data, is the wrong one: it always rewards the model that memorizes most. We need patients the model has never seen, used in a way that is neither wasteful nor a matter of luck. Cross-validation is the standard answer, and it is the reason a reported accuracy can be trusted.

1.2Prerequisites and notation

Prerequisites. Means, variances and covariances, matrix inverses, and the k-nearest neighbors classifier. Episode 2 (the bias–variance tradeoff, where overfitting was introduced) and Episode 4 (regularization, whose strength \lambda is tuned this way) are helpful.

Notation. \D is the training data, with N examples (x_i,y_i). A hyperparameter is written \lambda, and \hat\theta_\lambda(\D) are the parameters learned from \D with that setting. The data are cut into K folds \D_1,\dots,\D_K, and \D_{-j} is all the data except fold j. R(\theta,\D) is the average loss of a model on the data \D; for classification we usually report accuracy, which is one minus the average 0–1 loss. M is the number of candidate settings. The letter k is reserved for the number of neighbors.

1.3A short history

Larson (1931) noticed that a regression equation fitted on one sample predicts a fresh sample less well than its own: the multiple correlation “shrinks”. Checking a model on data held out from fitting starts there. Lachenbruch and Mickey (1968) estimated the error rate of a discriminant rule by leaving out one observation at a time. Stone (1974) set out the two jobs of cross-validation, choice (selecting a model or a setting) and assessment (estimating its error); Allen (1974) proposed the leave-one-out residual sum of squares for regression, PRESS, and Geisser (1975) the predictive sample reuse method, which leaves out groups instead of single points, the origin of K-fold cross-validation (Arlot and Celisse, 2010). Kohavi (1995) compared the methods in more than half a million runs of two learning algorithms on real data sets, and recommended stratified ten-fold cross-validation. The textbook treatments used here are Hastie et al. (2009, §7.10), James et al. (2013, §5.1) and Murphy (2022, §4.5.4–4.5.5). The data are the Wisconsin Diagnostic Breast Cancer measurements of Street et al. (1993). All numbers in this tutorial were computed for this tutorial with the same data and seeds as the companion video.