Chapter 10 — Assignment Project: Evaluating and Training a Digit Reader
Overview¶
A postal service wants to read handwritten digits (scikit-learn’s load_digits: 1 797 images of 8 × 8 pixels). Before choosing a model, they want to know how sure any reported accuracy is, which model is really better, whether more data would help, and whether the training of a neural network can be trusted.
| Part | Task | Points |
|---|---|---|
| A | How much does one split vary? | 20 |
| B | Comparing three models on the same folds | 20 |
| C | Learning curves | 20 |
| D | Controlling the training of a network | 25 |
| E | Report | 15 |
Rule: hold out a stratified 20% test set now (random_state=0) and use it only once, in Part D.
import numpy as np
import warnings
import matplotlib.pyplot as plt
from sklearn.datasets import load_digits
from sklearn.model_selection import train_test_split, cross_val_score, StratifiedKFold, RepeatedStratifiedKFold, learning_curve
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.neighbors import KNeighborsClassifier
from sklearn.svm import SVC
from sklearn.neural_network import MLPClassifier
from sklearn.metrics import log_loss
digits = load_digits()
X, y = digits.data / 16, digits.target
X_dev, X_test, y_dev, y_test = train_test_split(X, y, test_size=0.2, random_state=0, stratify=y)
models = {'logistic': make_pipeline(StandardScaler(), LogisticRegression(max_iter=5000)),
'k-NN': KNeighborsClassifier(n_neighbors=3),
'SVM': SVC(gamma='scale')}
print(X_dev.shape, X_test.shape)Part A — How Much Does One Split Vary? (20 points)¶
For the SVM, split the development set 30 times (80/20, stratified, seeds 0–29), and record the validation accuracy of each split. Report the minimum, the maximum and the standard deviation.
Run 5-fold stratified cross-validation on the development set. Report the mean and the standard deviation of the fold scores.
In two sentences: what does Part A tell you about comparing two models whose single-split accuracies differ by 0.005?
# Your code hereYour answer:
Part B — Comparing Three Models on the Same Folds (20 points)¶
Score the three models with
RepeatedStratifiedKFold(n_splits=5, n_repeats=5, random_state=0), the same splitter object for all.For each pair of models, report the mean paired difference, its standard deviation and the share of folds won.
Which model would you keep, and how confident are you?
# Your code hereYour choice and its justification:
Part C — Learning Curves (20 points)¶
Plot learning curves (training sizes 10% to 100% of the development set, 5-fold stratified) for logistic regression and the SVM.
For each, say whether the model is limited by bias or by variance, and whether doubling the data would be worth its cost.
# Your code hereYour reading of the curves:
Part D — Controlling the Training of a Network (25 points)¶
An MLPClassifier(hidden_layer_sizes=(128,), alpha=0, random_state=0) trained one epoch at a time with partial_fit on 80% of the development set, validated on the other 20%.
Train for 100 epochs with
learning_rate_init0.00001, 0.01 and 1.0. Plot the training and validation losses of each. Diagnose each curve with the vocabulary of Section 10.7.For the good learning rate, apply early stopping with a patience of 10 epochs (your own loop: keep a copy of the network at its best validation loss, with
copy.deepcopy).Report the test accuracy of the early-stopped network, once.
# Your code hereYour diagnoses:
Part E — Report (15 points)¶
Half a page for the postal service: the model you recommend; its expected accuracy with its uncertainty; what the learning curves say about collecting more data; and how the network’s training is controlled.
Grading Rubric¶
| Criterion | Excellent | Adequate | Insufficient |
|---|---|---|---|
| A (20) | spread measured and interpreted | numbers only | missing |
| B (20) | paired differences, a justified choice | means only | different folds per model |
| C (20) | curves read correctly for both models | curves without reading | missing |
| D (25) | three diagnoses, early stopping implemented, test used once | curves only | test set used to choose |
| E (15) | clear, with uncertainty | incomplete | missing |