Classical Machine Learning, lesson 7
Cross-validation and tuning
Pick settings on k folds, and keep the test set for the very end.
"Training, validation and test sets" (scores come from held-out rows) and "Regularisation with L1 and L2" (the penalty strength C). The code uses a scaled logistic regression from "Logistic regression".
Part 1. One split is one draw
scikit-learn ships a table of 569 breast tumours, each with 30 measurements and a label: malignant or benign. Hold back 20% as a test set. Split the remaining 455 rows into training and validation, fit a scaled logistic regression, and score it. Do that with five different random splits and the validation accuracy comes out 99.1%, 97.4%, 98.2%, 95.6% and 96.5%.
Nothing changed but which rows landed in validation. With 114 validation rows, each mistake moves the score by almost a point. Compare two settings on one split each and the split, not the setting, can decide the winner.
Your turn
Two settings of a model score 97.4% and 96.5%, each on its own random validation split of 114 rows. What can you conclude?
The fix is to use every row for validation, once. Cut the 455 rows into five folds of 91. Train on four folds and validate on the fifth, then repeat until each fold has had its turn. You get five scores, and you report their average and their spread.
Example 1.1
Five-fold cross-validation on 455 rows
- fold 1fold 2fold 3fold 4fold 590 of 91 right=0.989
Round 1: train on folds 2 to 5 (364 rows), validate on fold 1.
- fold 1fold 2fold 3fold 4fold 588 of 91 right=0.967
Round 2: a fresh model, trained on folds 1, 3, 4 and 5, validated on fold 2.
- round 389 of 91=0.978round 488 of 91=0.967round 590 of 91=0.989
Three more rounds, each fold scored by a model that never saw it.
- 445 ÷ 455=0.978 ± 0.010
Every row was validated exactly once. The average is 0.978, and the folds spread around it by 0.010 (their standard deviation).
Cross-validation
Cut the data into k folds; train on k − 1 of them and validate on the one left out, k times over, then average. Every row is validated once. Five and ten folds are the usual choices. Also called k-fold cross-validation.
Cross-validation costs k fits instead of one. With big data or slow models, people often use one large validation set instead: with hundreds of thousands of rows, a single split barely wobbles.
Part 2. Searching for settings
Keep going with Pro
You've done the first part. Pro opens the rest of this lesson, and every other one.
- Searching for settings
- Why the winner's score is too high
- Folds that fit the data
Classical Machine Learning overviewSpotted a mistake? Tell us.