Unit 1 of 10 · about 3 hours · implement
Holdout, 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.
You'll be able to
- Choose -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
Read
In this order, only the parts listed
0 of 3 done
- 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 and , 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)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.
Common mistakes · 3
- 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.
Try it yourself
Closed-book, before checking anything
SVC over and 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).Quiz yourself
Answer first, then mark it. Missed ones go to Review.
0 got · 0 missed
1.When does an integer
cvincross_val_scorestratify?AnswerWhen the
estimatoris a classifier andyis binary or multiclass, it usesStratifiedKFold; otherwise plainKFold. Neither shuffles unless asked.2.You select by CV and report the best CV score. What is wrong?Answer
Selection consumed the same scores, so the maximum is optimistic. Re-estimate with an outer loop (nested CV) or a sealed held-out set.
3.Each patient contributes several rows. Why is plain
KFoldwrong, and what replaces it?AnswerRows from one patient land on both sides and leak identity. Split by patient with
GroupKFoldorLeaveOneGroupOut.4.Why must scaling sit inside the CV loop?Answer
A scaler fit on all data lets test statistics shape training features. Each fold must fit the transform 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.
You're done 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.
Only when you can do this without notes.
Leads to
Notes
Resources · 2 for this unit, 5 more in the section
- scikit-learn - model selection and evaluation routescikit-learn · primary
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 testingJames, Witten, Hastie, Tibshirani and Taylor · required
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 InvestigationRoadmap-Final · reference
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 ShiftD2L authors · support
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 problemsDeep-ML · optional
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 tutorialsSciPy developers · reference
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 ClassificationJMLR · reference
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.