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 9 — Assignment Project: Tuning a Classifier Honestly

Open in Colab

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.

PartTaskPoints
ACapacity sweeps: a tree and k-NN25
BRegularisation: logistic regression’s C20
CSearch under a budget, and an honest estimate30
DRecommendation report25

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.


Part A — Capacity Sweeps (25 points)

  1. With validation_curve and cv above, compute the training and validation accuracy of a decision tree for max_depth from 1 to 12, and of a k-NN (inside a pipeline with StandardScaler) for kk in [1, 2, 3, 5, 8, 12, 20, 30, 50, 80].

  2. Plot both, and mark on each plot the region of underfitting, the region of overfitting, and your chosen value.

  3. Why does k-NN need the scaler, and the tree not? Show it: run k-NN with k=5k = 5 with and without scaling.

Your 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.

  1. For C in np.logspace(-4, 3, 8), compute the cross-validated training and validation accuracy of make_pipeline(StandardScaler(), LogisticRegression(C=C, max_iter=5000)).

  2. For each C, also report the sum of the absolute weights of the model fitted on the whole training set.

  3. Where does it underfit? Does it ever clearly overfit on this dataset? Explain why from the number of features and examples.

Your 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).

  1. Grid search: C in [0.1, 1, 10, 100], gamma in [1e-3, 1e-2, 1e-1, 1].

  2. Random search: 16 draws, C from loguniform(1e-1, 1e2), gamma from loguniform(1e-3, 1), random_state=0.

  3. Report each search’s best settings and best cross-validated score.

  4. Nested cross-validation: estimate the accuracy of the whole tuning procedure with cross_val_score(GridSearchCV(...), X_train, y_train, cv=outer), where outer = StratifiedKFold(5, shuffle=True, random_state=1). Compare it with the grid search’s best score. Which one is optimistic, and why?

  5. Finally, and only now, report the test accuracy of the model you choose.

Your 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

CriterionExcellentAdequateInsufficient
Capacity sweeps (25)both curves, regions marked, choices justified, scaling showncurves without interpretationmissing or using the test set
Regularisation (20)correct path, underfitting identified, explanation from n and dnumbers onlymissing
Search and estimate (30)both searches, nested CV, optimism explained, test used oncesearches onlytest set used to choose
Report (25)clear, honest number, limits statedincompletemissing