Model selection and cross-validation
Cross-validation, hyperparameter tuning, and model comparison
Model Selection and Cross-Validation
Model selection means choosing between competing workflows, not just algorithms. This lecture covers k-fold cross-validation in detail, hyperparameter tuning with GridSearchCV, choosing the right evaluation metric, and how to report model selection results. We emphasise that a reproducible selection rule is more valuable than an opaque “best model” claim.
Interactive: K-fold cross-validation
5-fold cross-validation: each iteration holds out a different fold for validation while training on the remaining four.
Re-run the wine comparison
Download the complete Python example and its pinned requirements into the same folder. The datasets are included with scikit-learn; no data download is needed. The published numbers were verified with Python 3.14.7 and scikit-learn 1.9.0.
In a fresh Python environment, run:
python -m pip install -r requirements-lecture-07.txt
python lecture_07_model_selection.pyThe default command runs the wine example: compare three families using training
CV, check a wider k-NN grid, apply the declared one-standard-error heuristic,
refit the selected configuration, and finally evaluate on the locked test set.
The selected logistic model uses C = 0.1; its CV macro-F1 is 0.993 ± 0.015 and its test macro-F1 is 1.000 on 36 wines. Fold standard deviation describes
variation across folds; it does not establish statistical equivalence.
The introductory binary and regression examples use their own datasets:
python lecture_07_model_selection.py --section binary
python lecture_07_model_selection.py --section regressionThe optional technical simulation reproduces
the choosing-k simulation (CV estimates for k = 3, 5, 10 and leave-one-out), the finer-grid one-SE example (which selects and refits C = 0.03), and the winner’s-curse experiment with matched five-fold inner
searches. It uses the same requirements and can take several minutes:
python lecture_07_cv_technical.py