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

08Interview questions and model answers

9 questions · click a question to see a brief answer
Q1Why can’t you choose a hyperparameter by training accuracy?
Brief answer

Training error is optimistic, and most optimistic for the most flexible model. 1-nearest neighbor scores 100\% on its training data because every point is its own nearest neighbor, yet 87.7\% on new patients. For squared error the optimism is (2/N)\sum_i\Cov(\hat y_i,y_i), which is 2\sigma^2/k for k-nearest neighbors, so training error always prefers k=1, as it prefers \lambda=0 for a regularizer (Murphy, 2022, §5.4.2).

Q2What is K-fold cross-validation, and how do you use it to tune a hyperparameter?
Brief answer

Lock away a test set. Cut the training data into K folds; for each candidate, train on K-1 folds and validate on the remaining one, rotating so that each fold is validated once, and average the K scores. Use the same folds for every candidate, pick the best average, refit once on all the training data, and evaluate once on the test set. With 10 candidates and 5 folds that is 10\times5+1=51 trainings; on our data the choice was k=41 (90.1\%) and the test accuracy 93.0\%.

Q3Why not use a single validation split?
Brief answer

Its score depends on which examples land in it: for 114 test patients the standard deviation is about \sqrt{p(1-p)/n}\approx2.6 to 2.9 points, and 200 splits of the same data gave 83.3\% to 96.5\% for the same model, while good candidates differed by about one point. It also trains on only part of the data. Cross-validation validates every example once and was 6.7 times steadier.

Q4How do you choose K? What about leave-one-out?
Brief answer

Larger K trains on more data (less pessimistic) but costs K trainings per candidate; the fold models of leave-one-out are nearly identical, so its estimate can be variable, and it can fail outright (Kohavi’s majority-class example). K=5 or 10 is the usual choice, stratified for classification (Kohavi, 1995). Leave-one-out is attractive when it is cheap, as for least squares, where \mathrm{CV}_{(N)}=\frac1N\sum_i\bigl((y_i-\hat y_i)/(1-h_{ii})\bigr)^2 needs a single fit.

Q5After cross-validation, which model do you deploy?
Brief answer

None of the fold models. Refit the chosen setting on all the training data (and, once the test score is recorded, it is common to refit on training and test data together for deployment). Cross-validation evaluated the recipe, not a particular fitted model.

Q6Is the cross-validation score of the best setting your final performance estimate?
Brief answer

No: it is the maximum of M noisy estimates, so it is biased upward, more so with many candidates and little data. Use a locked test set once, or nested cross-validation: an outer loop that holds out data while an inner loop does the tuning, at a cost of K_{\text{out}}(MK_{\text{in}}+1) trainings. On our data, nested cross-validation gave 90.0\%.

Q7What is data leakage in cross-validation? Give an example.
Brief answer

Information from the validation examples reaching the model through any step fitted on the whole data. The classic case (Hastie et al., 2009, §7.10.2): with 50 patients, 5{,}000 noise features and random labels, selecting the 100 features most correlated with the labels on all patients and then cross-validating gives about 1 to 3\% error, while the truth is 50\%. When we chose the 100 features from all 5{,}000, we had already used the labels of the patients in the validation folds. Redo every data-dependent step inside each fold, with a pipeline.

Q8How would you cross-validate data with several rows per patient, or a time series?
Brief answer

Keep all rows of one patient in the same fold (group K-fold), so the model cannot recognize patients it has seen. For time series, never shuffle: train on the past and validate on the following block, moving the window forward.

Q9Cross-validation is expensive. How can you reduce the cost?
Brief answer

The cost is MK+1 trainings. Use fewer candidates or a coarse-to-fine grid, use K=5 instead of 10, use a single large validation set when the data are plentiful or the model is very expensive, as with deep networks, and use closed forms where they exist: the leave-one-out shortcut and generalized cross-validation for linear smoothers (Hastie et al., 2009, §7.10.1).