2.6.15 · D1Model Evaluation & Selection

Foundations — Cross-validation pitfalls and nested CV

2,440 words11 min readBack to topic

This page assumes you have seen nothing but the word "data". We build every symbol the parent note (2.6.15) throws at you, one brick at a time. We introduce each symbol only in its own numbered section below — so by the time the parent hits its scary formulas, you will already know every letter in them.


1. What is a dataset? The symbol

The picture: a rectangle of rows. Each row is one example. We call the number of rows (the count of cards) and the number of input columns (the number of measurements per card).

Figure — Cross-validation pitfalls and nested CV

Why the topic needs it: everything — splitting, training, scoring — is an operation on this pile. If you don't picture as physical cards you can move into separate piles, none of the later steps make sense.


2. Splitting into folds — the symbols , fold , and

You cannot test a student on the exact questions they practised. So we hide some cards.

Figure — Cross-validation pitfalls and nested CV

Two more symbols fall straight out of the picture:

Why this notation? The parent writes . Read it now in plain English: "the model, using knob-setting , trained on all cards except fold ." Every subscript is earned.

This idea is developed fully in 2.6.2-K-fold-cross-validation — treat that as the prerequisite chapter.


3. A model and its knobs — the symbols and

The picture: a box with dials on the outside (, set by you) and gears on the inside (weights, learned from ).

Why the topic needs it: the whole disaster in the parent note is about choosing . You cannot talk about choosing a knob before you have a symbol for the knob. Knob-choosing strategies live in 2.6.12-Hyperparameter-tuning-strategies.


4. Measuring wrongness — the symbol

The picture: show the model each card in , compare its guess to the true back-of-card answer, and average the per-card mistakes.

Why not just "accuracy"? Loss is the general word — it could be mean squared error, mis-classification rate, anything. The topic uses "Loss" so the same formulas cover regression and classification without rewriting them.


5. Averaging the folds — the symbol and the CV score

Now we can read the parent's first formula. It only needs one new symbol.

Read the whole line aloud now, symbol by symbol: "The cross-validation score of knob-setting is the average, over every fold , of how wrong the model — trained on all-but-, tested on fold — turns out to be." Nothing here is mysterious anymore.

Why average? One fold could be lucky or unlucky. Averaging over folds smooths out that luck, giving a steadier estimate. This is the 2.7.3-Bias-variance-tradeoff tension in miniature: more folds = steadier average but more work.


6. Choosing the best knob — the symbols , and

You compute for several candidate settings and want the winner.

The picture: a bar chart, one bar per candidate (so bars), heights = CV scores. points at the height of the shortest bar; points at the label under it. The little star just means "the chosen, best one".

Figure — Cross-validation pitfalls and nested CV

7. Averages over unseen data — the symbol

The parent writes . One last symbol.

Why the topic needs it: "generalization" means how you do on new data on average — exactly . The complaint " is a biased estimate of " now reads plainly: your reported number does not match the true long-run average — it sits below it.


8. Two loops — inner vs outer

Everything above was one loop over folds. Nested CV wraps a second loop around it.

Figure — Cross-validation pitfalls and nested CV

Now the formal symbols for those two loops:

With these, the parent's nested formula reads plainly: run the inner search on to get , score it on the untouched , then average those scores over all rounds.


Prerequisite map

The diagram below feeds top to bottom: raw ideas at the top, the topic's punchline (nested CV) at the bottom. In words, the chain is: (a pile of cards) → split into folds → "train on , test on "; separately, a model with knobs produces a Loss; folds + Loss combine into the score; taking picks but sneaks in negative bias; comparing that against the true over new data exposes the bias; and the cure is nested CV's two loops (outer judges, inner chooses).

Dataset D as a pile of cards

K-fold split into folds

train on D minus k, test on fold k

Model f with knobs lambda

Loss measures wrongness

CV score averages the folds

arg min over m candidates picks lambda star

Bias sneaks in when you report the min

Expectation E over new data

Nested CV two loops outer judges inner chooses


Equipment checklist

Test yourself — reveal only after answering aloud.

What does the symbol mean in plain words?
All the folds except fold ; the pile you train on.
If and , what fold sizes do you use, and why?
— when you make the first few folds one card larger so every card lands in exactly one fold.
Why shuffle (and for classification, stratify) before dealing folds?
So sorted data doesn't dump all of one kind into one fold; stratifying keeps each fold's answer-proportions matching the whole pile.
What is the difference between a weight and a hyperparameter ?
A weight is learned from the training cards by the model itself; is a knob you set by hand before training.
Is a sum or an average over the cards, and why does it matter?
An average (mean over per-card losses); averaging keeps the scale the same whether has 20 or 2000 cards, so folds and the whole set are comparable.
Why do we average over folds instead of using one split?
One split can be lucky or unlucky; averaging smooths out that random luck into a steadier estimate.
What does count, and why does a bigger worsen the optimism?
= number of candidate knob settings tried; more candidates = more chances one dips below the true average by luck, and you always report that dip.
What is the difference between and ?
is the smallest score value; is the setting that produced it.
What does represent?
The true long-run average error on fresh, never-seen data — i.e. real generalization.
What are , and ?
Number of outer folds; the hidden outer fold (locked-drawer exam); everything except outer fold (what the inner loop studies on).
What does the superscript in mean?
"Belongs to outer fold " — each outer fold runs its own independent inner search.
In nested CV, what is the one job of the outer loop?
To judge the whole pipeline honestly on cards no knob-choice ever saw — never to select hyperparameters.