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

Holdout, cross-validation, nested selection and final test isolationGroup and temporal resampling with the correct sampling unit
1

Read

In this order, only the parts listed

0 of 3 done

  1. Cross-validation: computing metrics and choosing iteratorsscikit-learn - model selection and evaluation route

    Sections 3.1.1 (cross_validate versus cross_val_score, the Data transformation with held-out data note, 3.1.1.2 cross_val_predict and its warning) and 3.1.2 (K-fold, Stratified K-fold, Group K-fold, TimeSeriesSplit)

  2. Nested versus non-nested cross-validation on irisscikit-learn - model selection and evaluation route

    Whole example: inner GridSearchCV over and , outer cross_val_score; the non-nested best_score_ is optimistic next to the nested mean

  3. 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 KFold comparison)

    Skim the bootstrap half. It belongs to the uncertainty unit.

2

Focus on

  • An integer cv in cross_val_score means StratifiedKFold when the estimator is a classifier and y is binary or multiclass, and plain KFold otherwise, unshuffled in both cases.
  • cross_val_predict returns one out-of-fold prediction per row for inspection and stacking, but its pooled score is not a generalization estimate.
  • GroupKFold keeps whole groups out of training together; TimeSeriesSplit only ever tests on the future. The deployment unit picks the splitter.
  • Every fitted transform (scaling, imputation, selection) must sit inside a Pipeline under 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 a Pipeline so each fold fits transforms on its train split only.
3

Try it yourself

Closed-book, before checking anything

Closed-book, write the nested loop for an RBF 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).
4

Quiz yourself

Answer first, then mark it. Missed ones go to Review.

0 got · 0 missed

  1. 1.When does an integer cv in cross_val_score stratify?Answer

    When the estimator is a classifier and y is binary or multiclass, it uses StratifiedKFold; otherwise plain KFold. Neither shuffles unless asked.

  2. 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. 3.Each patient contributes several rows. Why is plain KFold wrong, and what replaces it?Answer

    Rows from one patient land on both sides and leak identity. Split by patient with GroupKFold or LeaveOneGroupOut.

  4. 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.

5

Practice

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, including paired=True, and Tutorial 2b (Paired-Sample Permutation Tests) with permutation_test and permutation_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.