Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Chapter 10 — Lab: Model Evaluation and Training Control

Open in Colab

Lab Overview

You will write the machinery behind KFold, StratifiedKFold and TimeSeriesSplit, run your own cross-validation, and build the two reflexes of a training loop: reading a loss curve and stopping early.

ExerciseTopicPoints
1k-fold indices15
2Cross-validation15
3Stratified folds20
4Time-series splits15
5Reading a loss curve15
6Early stopping with patience20
ChallengeA paired comparison+10

How it works. Each exercise has a function to complete where you see # TODO. Run the check cell under it: ✅ means your function is right, ❌ tells you what is wrong. Use NumPy and plain Python; scikit-learn appears only in the checks. Do not change the check cells.


Exercise 1: k-Fold Indices (15 points)

kfold(n, k) splits the indices 0 … n−1, in order, without shuffling, into k consecutive folds. The first n % k folds get one extra index (as scikit-learn’s KFold does). Return a list of k pairs (train_indices, val_indices) of NumPy arrays.


Exercise 2: Cross-Validation (15 points)

cross_val(score, n, k) calls score(train_indices, val_indices) for each fold of kfold(n, k) and returns (mean, standard deviation) of the scores (np.std, ddof=0).

The check uses it to cross-validate a polynomial fit on noisy sine data.


Exercise 3: Stratified Folds (20 points)

stratified_folds(y, k) returns a list of k arrays of validation indices. For each class, in sorted order, take that class’s indices in increasing order and deal them out to the folds in turn: the first to fold 0, the second to fold 1, …, the k-th to fold k−1, the next to fold 0 again. Return each fold’s indices sorted.


Exercise 4: Time-Series Splits (15 points)

time_splits(n, n_splits) returns n_splits pairs (train_indices, val_indices) as scikit-learn’s TimeSeriesSplit does: each validation block has n // (n_splits + 1) rows; the last block ends at row n − 1; the blocks follow each other; each training set is every row before its validation block.


Exercise 5: Reading a Loss Curve (15 points)

diagnose(losses) returns, checked in this order:

  • 'diverging' if any loss is not finite (np.isfinite) or the last loss is above the first;

  • 'stalled' if the last loss is above 90% of the first;

  • 'oscillating' if the loss went up on at least a third of the steps (a step is a pair of consecutive losses);

  • 'converging' otherwise.


Exercise 6: Early Stopping with Patience (20 points)

early_stop(val_losses, patience) walks through the validation losses. It remembers the lowest loss and its epoch (a new minimum must be strictly lower). It stops at the first epoch that is patience epochs after the best one, and returns (best epoch, stop epoch), both counted from 0. If it never stops, the stop epoch is the last one.

Question. Which patience would you choose for a validation loss measured on 50 examples, and which for one measured on 50 000? Why?


Challenge: A Paired Comparison (+10 points)

paired(a, b) takes the fold scores of two models on the same folds and returns (mean difference a − b, standard deviation of the differences, share of folds where a > b).


Lab Report

Submit this notebook with:

  • every check cell showing ✅,

  • your answer to the question of Exercise 6,

  • one paragraph: for a dataset you know, which splitter would you use (k-fold, stratified, group, time series), and why?