k-fold, LOOCV, choosing hyperparameters
Open the interactive lessons →Cross-validation and model comparison past questions
Contents
- Orientation: what you will learn and why
- Big picture: concept map, comparison table and the recipe
- Start here: a line through points
- Reading training and validation errors; choosing a hyperparameter
- How k-fold cross-validation works: counting models and sizes
- Leave-one-out cross-validation for a line
- Model comparison: zero training error, geometry and true/false facts
1. Orientation: what you will learn and why
A model that fits its own training data perfectly may still fail on new data. To choose settings (like $\lambda$, $k$ or tree depth) honestly we must test on data the model did not train on. Cross-validation does this by repeatedly holding out part of the data. This topic also collects the "which model can do what" questions that appear in many papers.
What you will be able to do
- Explain training, validation and test sets and recognise overfitting and underfitting from an error table.
- Choose a hyperparameter from a table of training and validation errors.
- Count how many models $k$-fold cross-validation trains and how big each training set is.
- Run leave-one-out cross-validation (LOOCV) for a line $y=w_0+w_1x$ by hand.
- Decide which models can certainly reach zero training error.
- Answer true/false questions that compare decision trees, k-NN, Naive Bayes and regression models.
Why this topic matters for Quiz 2
Counted only from the PYQs in this project (not a prediction):
| What the PYQs ask | PYQs | Marks |
|---|---|---|
| Choosing hyperparameters from error tables | 2 | 6 |
| k-fold cross-validation: counts | 2 | 3 |
| LOOCV for a line | 3 | 12 |
| Zero training error | 2 | 6 |
| Model comparison: true/false facts and geometry | 4 | 15 |
In total 13 PYQs worth 42 marks across 6 papers (5 MCQ, 4 MSQ, 4 numeric).
How this topic connects
It uses the line fit from the Linear regression topic, ridge from the Ridge topic, and k-NN and decision trees from their topics. The k-NN topic already showed LOOCV with a vote; here the model is a line.
Read each section, do the quick checks, then attempt each PYQ before opening its solution.
What is new in this upgrade [ADDED]
- A big picture section (next): concept map, train/validation/test table, k-fold against leave-one-out, counting table, mnemonics.
- Figures: the Aug 2025 table, five folds, and a four-point leave-one-out.
- Recall questions, a PYQ pattern map, variations and a flowchart. All new material is tagged [ADDED].
2. Big picture: concept map, comparison table and the recipe
[ADDED] Read this page first. It shows why we validate, how folds work and which facts the exam asks.
The concept map
Train, validation, test: three jobs [ADDED]
| Set | Used to | Used for choosing? | Used once? |
|---|---|---|---|
| Training | fit the weights / build the tree | no | many times |
| Validation | compare settings | yes | many times |
| Test | report the final score | no | once |
k-fold against leave-one-out [ADDED]
| k-fold ($k<n$) | Leave-one-out ($k=n$) | |
|---|---|---|
| Models trained | $k$ | $n$ |
| Training size | $n\frac{k-1}{k}$ | $n-1$ |
| Validation size | $n/k$ | $1$ |
| Cost | smaller | largest (n fits) |
| Estimate | a little pessimistic (less training data) | nearly unbiased, can vary more |
Counting, with examples [ADDED]
| $n$ | $k$ | Models (one setting) | Training size | Validation size |
|---|---|---|---|---|
| 100 | 5 | 5 | 80 | 20 |
| 1000 | 4 | 4 | 750 | 250 |
| 50 | 50 | 50 | 49 | 1 |
| 600 | 10 | 10 | 540 | 60 |
With $5$ candidate values of a hyperparameter and $k=4$, a full search trains $5\times4=20$ models. Read whether the question says "one value" or "all values".
Mnemonics and recognition cues [ADDED]
- "Validate to choose, test to report."
- "k models, not k times the data." Each model sees $n(k-1)/k$ points.
- "Leave it out, then fit." The left-out point must not touch the fit.
- "Low train, high validation = overfit; both high = underfit."
- Cue "each data point is its own neighbour": $1$-NN has zero training error (distinct points).
Analogy and its limit [ADDED]
| Analogy | Good for | Where it breaks |
|---|---|---|
| Practice exams: study from some, test yourself on the one you held back | why the held-out part must stay unseen | every fold is eventually used for training, so cross-validation scores are not a final test; keep a separate test set |
Recall questions
$n=100$, $k=5$: models and training size?
$5$ models, $80$ points each.
Leave-one-out on $n=50$: how many models?
$50$, each trained on $49$ points.
- Decide first which set the question is about: training, validation or test.
- Pick a hyperparameter by validation error, never by training error.
3. Start here: a line through points
A ruler laid through two nails: two points fix the line.
Piece 1. Fitting $y=w_0+w_1x$
$$w_1=\frac{S_{xy}}{S_{xx}},\qquad w_0=\bar y-w_1\bar x,\qquad S_{xx}=\sum(x_i-\bar x)^2,\ S_{xy}=\sum(x_i-\bar x)(y_i-\bar y)$$ $\bar x$ and $\bar y$ are the means of the $x$ and $y$ values; $w_1$ is the slope and $w_0$ the intercept of the least-squares line.
Problem. Points $(1,2)$ and $(0,1)$. Find $w_0,w_1$.
What is asked. The line.
Idea / formula used. Two points define a line exactly.
Problem. Points $(3,8),(0,3),(6,13)$. Find the least-squares line.
What is asked. $w_0,w_1$.
Idea / formula used. Means, then $S_{xx}$ and $S_{xy}$.
Piece 2. Three kinds of data
| Set | Used for | Used to choose settings? |
|---|---|---|
| Training | fit the weights | no |
| Validation | compare settings (hyperparameters) | yes |
| Test | final honest score | never |
- Through two points the line has slope $(y_2-y_1)/(x_2-x_1)$. Check it by putting one point back.
- Square each residual before adding; signs must not cancel.
Two points fix a line; residual is predicted minus actual.
4. Reading training and validation errors; choosing a hyperparameter
Intuition
A student who memorises last year's paper scores full marks on it (training) but poorly on a new paper (validation). The honest score is the new paper's, so that is what we use to choose between students (settings).
| Pattern | Training error | Validation error | Diagnosis |
|---|---|---|---|
| gap is big | very low | much higher | overfitting (too flexible) |
| both high | high | high | underfitting (too rigid) |
| both low and close | low | low | good fit |
Choose the setting with the lowest validation error. Training error alone is misleading because it keeps falling as the model becomes more flexible.
Worked examples
Problem. (learning rate, iterations): A $(0.01,2000)$ train $90$, validation $118$; B $(0.05,900)$ train $88$, val $95$; C $(0.2,600)$ train $70$, val $140$; D $(0.5,200)$ train $130$, val $138$.
What is asked. The best setting and why.
Idea / formula used. Choose by validation error; read the gap to name the problem.
Problem. $\lambda=0.01$: train $0.8$, val $5.6$; $0.5$: $1.4,\,2.9$; $2$: $2.2,\,2.4$; $20$: $4.9,\,5.0$; $200$: $9.8,\,10.1$.
What is asked. The best $\lambda$.
Idea / formula used. Lowest validation error.
Never pick a setting just because its training error is lowest. The validation error decides.
The Aug 2025 table as a picture [ADDED]
Diagnosis table [ADDED]
| Sign | Reading | What to try |
|---|---|---|
| train $\ll$ validation | overfitting | more regularisation, simpler model, fewer iterations |
| both high | underfitting | less regularisation, more flexible model |
| both low, close | good fit | keep |
| validation rises while training falls | overfitting has begun | stop earlier / pick the earlier setting |
Setting A: train 5, validation 30. Setting B: train 12, validation 14. Which?
B (lowest validation).
Can training error pick the best $\lambda$?
No, it keeps falling as flexibility grows.
- Choose the value with the smallest validation error, even if its training error is larger.
- Low training error with high validation error signals overfitting; both high signals underfitting.
Train to fit, validate to choose, test to report.
5. How k-fold cross-validation works: counting models and sizes
Intuition
Split a class into $k$ equal study groups. Let each group take its turn as the "exam group" while the other $k-1$ groups are used to study. Everyone gets examined exactly once.
Split the $n$ points into $k$ equal parts (folds). For each fold $j=1,\dots,k$: train on the other $k-1$ folds, evaluate on fold $j$. Average the $k$ validation errors.
$$\text{models trained (for one setting)}=k,\qquad \text{training size}=n\cdot\frac{k-1}{k},\qquad \text{validation size}=\frac nk$$ $n$ is the number of data points and $k$ the number of folds. Leave-one-out is the special case $k=n$: $n$ models, each trained on $n-1$ points.
If you test several values of a hyperparameter (say $5$ values of $\lambda$), the total number of models is $5\times k$. The question wording says whether it asks for "one value of $\lambda$" or for all.
Problem. $n=1000$, $k=4$ folds, one value of $\lambda$. How many models are trained and how big is each training set?
What is asked. Both numbers.
Idea / formula used. One model per fold.
Problem. $n=50$ points, leave-one-out. Models and training size?
What is asked. Both numbers.
Idea / formula used. $k=n$.
Five folds, drawn [ADDED]
Counting slips [ADDED]
| Slip | Correct |
|---|---|
| training size $=n/k$ | $n(k-1)/k$ (that is the validation size plus the rest) |
| models $=n$ for $k$-fold | $k$ models |
| forgetting the number of settings | total $=\text{settings}\times k$ |
$n=600$, $k=10$: validation size?
$60$.
Which $k$ gives leave-one-out?
$k=n$.
Each row is one training run. Yellow points are held out for validation in that run, blue points train the model. Set $k=n$ to see leave-one-out.
- Models trained = number of folds times number of candidate values. Add one only if the question also retrains on all data.
- With $n$ points and $k$ equal folds, each model trains on $n(k-1)/k$ points.
Models trained: folds times candidate values.
6. Leave-one-out cross-validation for a line
Each pupil takes a turn as the examiner's surprise question while the others are the study material.
Procedure
- Take the left-out point; remove it.
- Fit the line $y=w_0+w_1x$ on the remaining points.
- Predict the left-out point: $\hat y=w_0+w_1x_{out}$.
- Compare with the true $y_{out}$ (squared error $(\hat y-y_{out})^2$).
- Repeat for each point and average the errors.
Worked examples
Problem. $D=\{(1,1),(3,5),(2,0)\}$, model $y=w_0+w_1x$. Leave out $(2,0)$.
What is asked. $\|w\|_2^2$ and the squared error on the left-out point.
Idea / formula used. Fit the line on the other points, then test it once.
Problem. $(3,8),(0,3),(5,12),(6,13)$. Run LOOCV.
What is asked. Fitted line and prediction for each left-out point.
Idea / formula used. Refit four times.
When a question says "the 3rd sample is the validation point", number from $1$ in the table order: here sample 3 is $(5,12)$.
The four-point LOOCV, drawn [ADDED]
| Left out | Prediction | True | Squared error |
|---|---|---|---|
| (3, 8) | 8.194 | 8 | 0.037 |
| (0, 3) | 3.000 | 3 | 0.000 |
| (5, 12) | 11.333 | 12 | 0.444 |
| (6, 13) | 13.632 | 13 | 0.399 |
Mean of the four squared errors: 0.220. A large error flags a point that the rest of the data cannot predict (here $(5,12)$).
In LOOCV, is the left-out point used to fit its own line?
No, never.
- With three points, each leave-one-out line passes through the other two, so no fitting formula is needed.
- Average the squared errors over all left-out points unless the question asks for the sum.
Leave one out, fit the rest, square the miss, average.
7. Model comparison: zero training error, geometry and true/false facts
A suit cut to every crease of one person fits nobody else.
Which models can reach zero training error?
Zero training error means every training point is predicted correctly (or exactly, for regression).
| Model | Zero training error guaranteed? | Why |
|---|---|---|
| Look-up rule $h(x_i)=y_i$ | Yes | it repeats the label |
| Decision tree (grown fully, distinct points) | Yes | keep splitting until leaves are pure |
| $1$-NN (each point its own neighbour) | Yes | nearest point is itself |
| $k$-NN with $k=5$ | No | a point can be outvoted |
| Naive Bayes | No | its assumptions can force mistakes |
| Linear model $w^Tx+b$ | No | a straight line cannot fit every pattern, even if $n>d+1$ |
| Predict the mean $\frac1n\sum y_j$ or $0$ | No | ignores the input |
Geometry of the decision or fit
| Model | Boundary shape |
|---|---|
| Decision tree | axis-aligned (splits $x_j\le t$) |
| Ridge and Lasso regression | linear (a hyperplane) |
| $1$-NN | non-linear, piecewise |
| Gaussian Naive Bayes (different variances per class) | non-linear, curved (quadratic) |
True / false facts that keep appearing
| Statement | True? |
|---|---|
| Decision trees are a supervised method for classification | True |
| k-NN is lazy (no explicit model built at training) | True |
| In k-NN $k$ is the number of features | False ($k$ counts neighbours) |
| k-NN is parametric and needs fitted distributions | False |
| Depth of a tree is a hyperparameter, typically chosen by cross-validation | True |
| A question $f_k\le\theta$ always splits the data into two non-empty sets | False (if $\theta$ is outside the data range one side is empty) |
| A "better" question has greater information gain | True (that is how "better" is defined) |
| Trees overfit if the maximum depth is too low | False (they underfit) |
| Trees underfit if the maximum depth is too low | True |
| The training set is not needed to predict with k-NN | False (it is needed) |
Too deep $\Rightarrow$ overfit. Too shallow $\Rightarrow$ underfit. A "too low depth overfits" option is always false.
Why each model can or cannot reach zero training error [ADDED]
| Model | Distinct points, any labels | Reason |
|---|---|---|
| fully grown tree | yes | keep splitting to pure leaves |
| $1$-NN (own neighbour) | yes | nearest point is itself |
| $k$-NN, $k\ge2$ | no | outvoted by neighbours |
| linear classifier | no | XOR-type labels cannot be split by a line |
| naive Bayes | no | independence can force mistakes |
Why can a linear classifier fail even with distinct points?
Labels in an XOR pattern are not separable by a single line.
Four points $x=1,2,4,8$ with labels $0,1,0,1$. 1-NN: every point is its own nearest neighbour, so training error is $0$. 3-NN (each point counts itself): $x=2$ has neighbours $2,1,4$ with labels $1,0,0$, so it is predicted $0$ and is wrong; the other three are right, so training error is $1/4$. One threshold $x\le\theta$ cannot reproduce the pattern $0,1,0,1$, so it makes at least one training mistake.
- Zero training error says nothing by itself about new data.
- Test each true or false line with the extreme case of a very flexible model.
Perfect training fit proves nothing about new data.