2.6.4 · D4Model Evaluation & Selection

Exercises — K-fold cross-validation

2,956 words13 min readBack to topic

Before we start, let us make sure every symbol on this page is earned.

Figure — K-fold cross-validation

Look at the figure: the horizontal strip is your whole dataset . One coloured box is the test fold (); the rest are the training folds (). As moves from to , the coloured box slides along — that is the entire algorithm in one picture.


L1 — Recognition

Recall Solution 1.1

WHAT: Each fold is an equal pile, so it holds samples. WHY that many models: In K-fold we train one model per fold (each fold takes a turn as the test set). So the number of models trained equals . Answer: samples per fold, models trained.

Recall Solution 1.2

False. In any single iteration , the folds are disjoint: the test fold is completely removed from training (, i.e. "all of except box "). A point is either being tested or being trained on in that iteration — never both at once. (Across different iterations a point is used for both, just not simultaneously.)

Recall Solution 1.3

means each fold has exactly one sample (). Testing on one point at a time, using all others to train, is called Leave-One-Out Cross-Validation (LOOCV). Note this is the largest legal , since .


L2 — Application

Recall Solution 2.1

WHAT: average the four scores. Answer: (i.e. ).

Recall Solution 2.2

WHY subtract the mean first: measures spread around the average, so every score is compared to .

Sum . Divide by : . Take the square root: Answer: , so the model is .

Recall Solution 2.3

Each fold = test samples. Training uses the other folds: Fraction . Answer: training samples, of the data.


L3 — Analysis

Recall Solution 3.1

Compute the spread. Model A's scores hug tightly; Model B swings from to . Model A : deviations → sum of squares . Model B : deviations → squares → sum . Interpretation: same average, but . Model B is wildly sensitive to which data it trains on — a red flag (unstable model or non-representative folds). Trust Model A: consistent scores mean the estimate is reliable.

Recall Solution 3.2

The core idea: more training data usually → better model.

  • : each CV model trains on samples. That is far short of the final , so those models are weaker than the deployed one → the reported score underestimates true performance. That downward gap is the bias.
  • : each CV model trains on samples — much closer to → its performance closely mirrors the final model → less bias. Trade-off summary: larger shrinks bias but averages over noisier, more-overlapping training sets (higher variance) and costs trainings.
Recall Solution 3.3

The failure: random splitting could dump many/all of the class-B points into just one or two folds. A validation fold with zero class-B points can only measure class-A accuracy — the minority class is invisible, so the score is misleading (a model that ignores class B could still look "good"). The fix — stratified K-fold: keep each fold at the same ratio as the whole set. For : each fold gets class-A and class-B samples. Now every validation fold contains both classes and truly represents the data distribution.


L4 — Synthesis

Recall Solution 4.1

Correct procedure:

  1. Shuffle once, then split it into 5 folds and fix that split.
  2. For each setting (say and ): a. For each fold : train the model with that setting on the other 4 folds. b. Do all preprocessing (scaling, imputing) fitted on the training folds only, then apply to . c. Record . d. Compute for that setting.
  3. Pick the setting with the higher .
  4. Retrain that chosen setting on all data for deployment. The classic leak: fitting the scaler / choosing the hyperparameter using information from the validation fold (e.g. normalizing the whole dataset before splitting). Preprocessing statistics must be learned inside each training fold only.
Recall Solution 4.2

Split each class into 5 equal parts:

  • Class A: per fold.
  • Class B: per fold. One fold : A B samples, preserving the ratio. Training size per model: samples (which is A B A B — still ). Answer: each fold samples (A/B); each model trains on samples.

L5 — Mastery

Recall Solution 5.1

LOOCV means folds, one point each. The per-fold "score" here is the squared error. Fold spread: deviations from : . Squares: , sum . Divide by : . Square root: Answer: LOOCV MSE , . The large spread relative to the mean warns that with only points the estimate is very shaky — exactly the "maximum variance" LOOCV is known for.

Recall Solution 5.2

When the scaler's mean/standard-deviation are computed on the full dataset, each validation fold's statistics have already influenced the numbers the model trains on — the model gets a subtle hint about the test data. This inflates performance, so is optimistically biased. The corrected pipeline (fit scaler on training folds only, apply to the held-out fold) removes that hint and gives an honest . Publish — it estimates true generalization. Reporting would over-promise and the model will underperform in production.

Recall Solution 5.3

Each strategy trains models, so total time minutes.

  • (a) : minutes.
  • (b) : minutes.
  • (c) LOOCV, : minutes hours. Rule: cost grows linearly in . LOOCV's tiny bias rarely justifies a cost jump, so or is the default; reserve LOOCV for very small datasets where every sample is precious.

Recall Quick self-check (cloze)

The two aggregation formulas are ::: (the mean of fold scores). Each K-fold model trains on a fraction ::: of the data. LOOCV corresponds to ::: (one sample per fold; the largest legal ). Before splitting into folds you should always ::: shuffle the data once (so sorted data does not bias the folds). A large fold spread with a good mean means ::: the model is unstable across folds — do not trust the single number. Fitting a scaler on the whole dataset before splitting causes ::: data leakage (optimistically biased scores).