Exercises — Train - validation - test splitting
This page is a self-test. Each problem has a full worked solution hidden inside a collapsible callout — try it first, then open. Problems climb from L1 Recognition (just spotting the right idea) up to L5 Mastery (putting many ideas together correctly under pressure). Everything you need was built in the parent note; where a tool appears, we re-explain it here from zero.
L1 — Recognition
Goal: name the right set for the right job. No arithmetic yet.
Exercise 1.1
You are choosing between a random-forest with 100 trees and one with 500 trees. On which set do you compare their scores to pick the winner, and on which set are you forbidden to peek until the very end?
Recall Solution 1.1
Compare on the validation set. The test set stays sealed until the single final measurement. Why: picking the better of two models is a decision made from data. Any set used to make a decision has been "trained on" in a soft sense, so it can no longer give an honest estimate of unseen performance. The test set is the only set that has influenced zero decisions — that is what makes its number trustworthy.
Exercise 1.2
Match each phrase to Train / Validation / Test: (a) "fits the weights," (b) "tunes the learning rate," (c) "reported in the paper as final accuracy."
Recall Solution 1.2
(a) Train — weights are parameters, learned by the fitting procedure. (b) Validation — the learning rate is a hyperparameter, chosen by comparing runs. (c) Test — the last, untouched measurement.
L2 — Application
Goal: turn split ratios into actual counts, with rounding done right.
Exercise 2.1
A dataset has samples. Apply an split. How many samples land in each set?
Recall Solution 2.1
train, val, test. Check the total: . ✓ Why check the sum? Splits must be mutually exclusive and exhaustive — every sample belongs to exactly one set, none is lost or duplicated.
Exercise 2.2
Same , but a split. Give the counts.
Recall Solution 2.2
, , . Sum: . ✓
Exercise 2.3
The parent note uses a two-step trick to reach : first peel off as test, then split the remaining so that the val slice equals of the original. What fraction of the remaining set must the second split remove to get that ? (Use .)
Recall Solution 2.3
After removing test, the remaining pool is .
We want the val set to be of the original samples.
That is a fraction of the remaining pool.
Why the number changes: of the whole is a bigger slice of the smaller leftover — the denominator shrank from to , so the same 500 samples take up a larger share. This is why the scikit-learn code passes test_size=0.125 on the second call, not 0.10.
L3 — Analysis
Goal: reason about stratification and confidence, not just counts.
Exercise 3.1
A medical dataset: images, healthy, diseased. You do a stratified split. How many diseased images are in each set?
Recall Solution 3.1
Stratification splits each class separately so proportions are exact by construction. Diseased count .
- Train:
- Val:
- Test: Check: . ✓ Each split is diseased, matching the whole. See the figure — each colour band is cut in the same ratio.

Exercise 3.2
"95% confidence interval" (CI) means: if we repeated the whole experiment many times, the true accuracy would fall inside our reported interval about 95 times out of 100. Its half-width for an accuracy measured on test samples is Why this formula? Accuracy is a fraction of successes among independent trials — a proportion. The spread of a proportion shrinks like , and is the number of standard deviations that captures the middle of a bell curve. Compute the half-width for test samples at the worst-case .
Recall Solution 3.2
Why is "worst case": the product is largest at (a coin flip is the noisiest possible outcome), so this gives the widest interval — a safe upper bound on your uncertainty.
Exercise 3.3
Your team wants the test-set half-width (at ) down to . How many test samples do you need?
Recall Solution 3.3
Set and solve. . So you need about test samples. Why so many? Halving the width from to requires 4× the data, because error falls as : to divide error by 2 you multiply by . This is exactly why the parent note stops adding test data past a point — the returns shrink brutally.
L4 — Synthesis
Goal: combine splitting with preprocessing and spot leakage across steps.
Exercise 4.1
A teammate writes:
scaler.fit(X_all) # all 5000 rows
X_train_s = scaler.transform(X_train)
X_test_s = scaler.transform(X_test)Name the flaw, name the vault topic it violates, and give the corrected two lines.
Recall Solution 4.1
Flaw: the scaler's mean and standard deviation were computed from all rows — including test rows. The training pipeline has now indirectly seen the test set through those statistics. This is data leakage (6.3.2-Data-leakage), and it inflates the test score. Corrected:
scaler.fit(X_train) # statistics from TRAIN only
X_train_s = scaler.transform(X_train)
X_test_s = scaler.transform(X_test) # apply train's μ, σRelated: 2.1.11-Feature-scaling-normalization explains what and do; here the point is when to compute them.
Exercise 4.2
Given , churn target with positives, stratified . State how many positive samples end up in the validation set, and then argue whether validation samples with positives is "enough" to compare 5 candidate models.
Recall Solution 4.2
Positives total . Validation is of the data, so validation positives . Validation size , with positives. Enough? For comparing a handful of models, yes — you only need to distinguish which is better, and samples give a val-accuracy half-width around at worst case. That resolves models that differ by more than a few percent. But with only positives, any metric that depends on the minority class (like recall) is noisy — treat close calls with suspicion and consider 2.1.13-Cross-validation for small pools.
L5 — Mastery
Goal: full pipeline decisions where several rules interact, plus edge cases.
Exercise 5.1
You have labelled examples, badly imbalanced ( positive = positives total). You plan an split. Show by counting why this split breaks, then state the better strategy and why.
Recall Solution 5.1
Val and test are each : samples each, containing positive each (after floor). So validation and test each hold one positive example. Measuring recall on one positive means your metric can only be or — it carries almost no information, and its confidence interval is enormous. Better strategy: for use ==-fold cross-validation== (2.1.13-Cross-validation) with stratified folds, so every example gets to be tested exactly once and the minority class is spread across folds. The parent's rule of thumb (" → cross-validation") exists precisely to avoid single-digit test classes.
Exercise 5.2
You run 100 model variants, pick the best validation accuracy of , then report test accuracy of . Your manager asks why the "real" number dropped points. Explain the mechanism and whether or is the honest estimate.
Recall Solution 5.2
Mechanism: with 100 variants, the best validation score is partly luck — you selected the model that happened to fit the validation set's noise best (this is validation overfitting, a cousin of 3.2.1-Overfitting-underfitting). The maximum of many noisy numbers is biased upward. The test set, untouched by that selection, reveals the truth. Honest estimate: . The test set is the only number that had zero influence on which model you chose. is optimistically biased by the selection process — exactly the information-leakage-through-model-selection argument the parent note derives. Connected idea: if you also use validation to decide when to stop training (4.1.5-Early-stopping), the same caution applies — the stopping point is chosen from val, so val is not a clean generalization estimate either.
Exercise 5.3
Confirm the parent note's claim: with test samples, the worst-case CI half-width for accuracy is about .
Recall Solution 5.3
This is why past k samples you can drop the test fraction to or less — k test rows already pin accuracy to within a percent.
Recall Quick self-check (cloze)
The set used to choose hyperparameters ::: validation set The set that may influence zero decisions until the end ::: test set To halve a CI half-width you multiply by ::: 4 Preprocessing statistics () must be computed from ::: the training set only For , prefer this over a fixed split ::: cross-validation