Chapter 9 — Assignment Project: Tuning a Classifier Honestly
Overview¶
A winery wants a classifier that tells its three grape cultivars apart from 13 chemical measurements (scikit-learn’s load_wine, 178 wines). You will map how each model’s capacity moves it from underfitting to overfitting, tune it with a fixed budget, and give the winery a number it can trust.
| Part | Task | Points |
|---|---|---|
| A | Capacity sweeps: a tree and k-NN | 25 |
| B | Regularisation: logistic regression’s C | 20 |
| C | Search under a budget, and an honest estimate | 30 |
| D | Recommendation report | 25 |
Rules: hold out 25% of the data as a test set now, stratified, with random_state=0, and do not touch it before Part C’s final step.
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import load_wine
from sklearn.model_selection import train_test_split, validation_curve, cross_val_score, GridSearchCV, RandomizedSearchCV, StratifiedKFold
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.tree import DecisionTreeClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.svm import SVC
from scipy.stats import loguniform
X, y = load_wine(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=0, stratify=y)
cv = StratifiedKFold(5, shuffle=True, random_state=0)
print(X_train.shape, X_test.shape)Part A — Capacity Sweeps (25 points)¶
With
validation_curveandcvabove, compute the training and validation accuracy of a decision tree formax_depthfrom 1 to 12, and of a k-NN (inside a pipeline withStandardScaler) for in[1, 2, 3, 5, 8, 12, 20, 30, 50, 80].Plot both, and mark on each plot the region of underfitting, the region of overfitting, and your chosen value.
Why does k-NN need the scaler, and the tree not? Show it: run k-NN with with and without scaling.
# Your code hereYour diagnosis and choices:
Part B — Regularisation (20 points)¶
Logistic regression’s hyperparameter C is the inverse of the penalty strength: a small C is a strong penalty.
For
Cinnp.logspace(-4, 3, 8), compute the cross-validated training and validation accuracy ofmake_pipeline(StandardScaler(), LogisticRegression(C=C, max_iter=5000)).For each
C, also report the sum of the absolute weights of the model fitted on the whole training set.Where does it underfit? Does it ever clearly overfit on this dataset? Explain why from the number of features and examples.
# Your code hereYour explanation:
Part C — Search Under a Budget, and an Honest Estimate (30 points)¶
An RBF SVM in a pipeline with StandardScaler. Your budget is 16 settings of (C, gamma).
Grid search:
Cin[0.1, 1, 10, 100],gammain[1e-3, 1e-2, 1e-1, 1].Random search: 16 draws,
Cfromloguniform(1e-1, 1e2),gammafromloguniform(1e-3, 1),random_state=0.Report each search’s best settings and best cross-validated score.
Nested cross-validation: estimate the accuracy of the whole tuning procedure with
cross_val_score(GridSearchCV(...), X_train, y_train, cv=outer), whereouter = StratifiedKFold(5, shuffle=True, random_state=1). Compare it with the grid search’s best score. Which one is optimistic, and why?Finally, and only now, report the test accuracy of the model you choose.
# Your code hereYour comparison:
Part D — Recommendation Report (25 points)¶
One page for the winery:
the model you recommend, its hyperparameters, and why (Parts A–C);
the accuracy they should expect, and why it is the nested or test estimate, not the best search score;
what would change your recommendation (more wines, a fourth cultivar, a measurement that drifts over the years).
Grading Rubric¶
| Criterion | Excellent | Adequate | Insufficient |
|---|---|---|---|
| Capacity sweeps (25) | both curves, regions marked, choices justified, scaling shown | curves without interpretation | missing or using the test set |
| Regularisation (20) | correct path, underfitting identified, explanation from n and d | numbers only | missing |
| Search and estimate (30) | both searches, nested CV, optimism explained, test used once | searches only | test set used to choose |
| Report (25) | clear, honest number, limits stated | incomplete | missing |