Unit 5.1 · about 3 h
160 / 324Holdout, cross-validation and resampling by the right unit
A validation score that guided model choice is optimistic. The sampling unit decides what the error bars mean at deployment.
- implement
You can now
- Choose \(K\)-fold, stratified, group or time-series resampling from the deployment dependence structure and name the sampling unit.
- Run nested selection so the reported score never guided the choice it evaluates.
- Keep preprocessing inside the resampling loop so no test information leaks into fitting.
Covers
- Holdout, cross-validation, nested selection and final test isolation
- Group and temporal resampling with the correct sampling unit
Study, in this order
3 steps
- Cross-validation: computing metrics and choosing iteratorsscikit-learn - model selection and evaluation route
Sections 3.1.1 (
cross_validateversuscross_val_score, the Data transformation with held-out data note, 3.1.1.2cross_val_predictand its warning) and 3.1.2 (K-fold, Stratified K-fold, Group K-fold,TimeSeriesSplit) - Nested versus non-nested cross-validation on irisscikit-learn - model selection and evaluation route
Whole example: inner
GridSearchCVover \(C\) and \(\gamma\), outercross_val_score; the non-nestedbest_score_is optimistic next to the nested mean - ISLP Chapter 5 lab: validation-set, LOOCV and k-fold errorsISLP - resampling and multiple testing
The Validation Set Approach (linear/quadratic/cubic MSE triple 23.62, 18.76, 18.80 on Auto) and Cross-Validation (LOOCV MSE 24.23, then the 10-fold
KFoldcomparison)Depth · Skim the bootstrap half. It belongs to the uncertainty unit.
Focus on
- An integer
cvincross_val_scoremeansStratifiedKFoldwhen theestimatoris a classifier andyis binary or multiclass, and plainKFoldotherwise, unshuffled in both cases. cross_val_predictreturns one out-of-fold prediction per row for inspection and stacking, but its pooled score is not a generalization estimate.GroupKFoldkeeps whole groups out of training together;TimeSeriesSplitonly ever tests on the future. The deployment unit picks the splitter.- Every fitted transform (scaling, imputation, selection) must sit inside a
Pipelineunder CV, or test statistics shape the training features.
Reproduce closed-book
SVC over \(C \in \{1, 10, 100\}\) and \(\gamma \in \{0.01, 0.1\}\) on iris, with KFold(4, shuffle=True) inner (random_state=0) and outer (random_state=1) splits, and state which number is honest. Reference run: nested mean 0.9468 against non-nested best_score_ 0.9603. One trial is noisy: the docs example averages 30 trials and finds a mean gap of only 0.0076 (std 0.0078).Traps
- Reporting
GridSearchCV.best_score_as model performance. It was maximized over the same folds, so the nested mean is the honest estimate. - Splitting rows when the deployment unit is a group or a time block. Shared patients, users or days across train and test inflate the score.
- Fitting the scaler or selector on all data before
cross_val_score. Wrap it in aPipelineso each fold fits transforms on its train split only.
Practice
ISLP Ch05 lab: validation-set and cross-validation errors
Reproduce the linear/quadratic/cubic validation MSE triple and the LOOCV estimate 24.23 on Auto, then the 10-fold comparison
Match the printed numbers before trusting your own splitter on grouped or temporal data.
Self-test
-
When does an integer
cvincross_val_scorestratify?When theestimatoris a classifier andyis binary or multiclass, it usesStratifiedKFold; otherwise plainKFold. Neither shuffles unless asked. -
You select \(C\) by CV and report the best CV score. What is wrong?
Selection consumed the same scores, so the maximum is optimistic. Re-estimate with an outer loop (nested CV) or a sealed held-out set. -
Each patient contributes several rows. Why is plain
KFoldwrong, and what replaces it?Rows from one patient land on both sides and leak identity. Split by patient withGroupKFoldorLeaveOneGroupOut. -
Why must scaling sit inside the CV loop?
A scaler fit on all data lets test statistics shape training features. Each fold must fit the transform on its train split only.
Completion criterion
Stop when
You reproduce the nested versus non-nested gap on iris, and you can name the sampling unit and the nested structure for a grouped or temporal problem without notes.
Mark passed, then open 5.2 Only mark this when you can do it without notes.Leads to
- 5.6 Paired bootstrap, permutation tests and multiplicity Put paired uncertainty on the resampled estimates.
- 1.3 Split design, nested selection and test isolation Match the split design to the same dependence structure.
Comes after
- 4.1 Estimands, estimators, bias, variance and consistencyApply sampling-distribution reasoning when rows are not independent units.
- 4.6 Missingness and selection mechanismsCheck population selection before treating observed units as representative.
- 4.7 Randomized experiment design, power and MDERespect dependence and randomization units in uncertainty estimates.
- 4.8 Causal claims, causal graphs, adjustment and identificationKeep identification separate from sampling uncertainty and population selection.
- 4.8 t-SNE and UMAP as guarded exploratory viewsMove from exploratory views to evaluation, starting with the sampling unit behind every estimate.
Resources for this unit
- scikit-learn - model selection and evaluation route Follow Cross-validation, Hyperparameter tuning, Decision threshold tuning, Metrics and scoring, Probability calibration, Validation curves, Permutation importance and Common Pitfalls. Retain raw predictions and independently hand-check central metrics.
- ISLP - resampling and multiple testing Use Chapter 5 and the relevant selection-history parts of Chapter 13 with their Python labs. Connect resampling to uncertainty and multiplicity without repeating the full statistics stage.
Also in this section
- Classical ML Scientific Investigation Use Core requirements, architecture constraints, Milestones A-F and failure injection as the standard for end-to-end evidence. The full project remains a later capstone route.
- D2L - Environment and Distribution Shift Read 4.7, then construct one controlled covariate, label and concept shift. Record what changes, which signal detects it and what response the evidence supports.
- Deep-ML candidate - evaluation problems After the platform admission check, use at most one of 18 K-Fold CV, 260 Expected Calibration Error, 276 ROC, 277 AUC or 840 Stratified K-Fold. Hand-checked local metrics remain the evidence.
- SciPy - Resampling and Monte Carlo methods tutorials Work the notebooks linked from this page: Tutorial 3 (The Bootstrap) with
scipy.stats.bootstrap, includingpaired=True, and Tutorial 2b (Paired-Sample Permutation Tests) withpermutation_testandpermutation_type='samples'. Keep one resampling index stream across compared models. - El-Yaniv and Wiener - On the Foundations of Noise-free Selective Classification Read the Abstract and Section 2 (Preliminary Definitions: Definitions 1 and 2 for coverage and selective risk) for the risk-coverage tradeoff. Stop before the noise-free learning bounds.