Sub topic 12
Using curves in a tuning workflow
This chapter assumes you can compute and read both curve types with learning_curve and validation_curve. It also assumes you know the bias and variance diagnoses and how a held-out test set differs from cross-validation folds. Here you will put the curves in order and learn where each one fits in a tuning process.
The order of operations
Each curve answers a different question, so the order matters.
- Compute a learning curve for a reasonable default model. It tells you whether the problem is high bias, high variance, or neither.
- Pick the hyperparameter that controls flexibility. Compute a validation curve for it.
- Choose a value, refit, and re-check the learning curve.
- Score once on a held-out test set.
Choosing a value from a validation curve
The highest mean validation score is not automatically the best choice. Fold scores are noisy, so a value slightly below the peak may be statistically indistinguishable from it. A common rule is to take the simplest setting whose mean score is within one standard deviation of the best. Simpler models tend to be cheaper and more stable.
The code below applies that rule to the gamma of an RBF SVM. A smaller gamma gives a smoother model, so the first qualifying value in an ascending range is the simplest.
import numpy as np
from sklearn.datasets import load_digits
from sklearn.model_selection import train_test_split, validation_curve
from sklearn.svm import SVC
X, y = load_digits(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.25, stratify=y, random_state=0
)
param_range = np.logspace(-6, -1, 6)
train_scores, val_scores = validation_curve(
SVC(kernel="rbf"),
X_train,
y_train,
param_name="gamma",
param_range=param_range,
cv=5,
scoring="accuracy",
)
val_mean = val_scores.mean(axis=1)
val_std = val_scores.std(axis=1)
best = val_mean.argmax()
threshold = val_mean[best] - val_std[best]
chosen = param_range[np.where(val_mean >= threshold)[0][0]]
final = SVC(kernel="rbf", gamma=chosen).fit(X_train, y_train)
print(f"best gamma={param_range[best]:.0e}, chosen gamma={chosen:.0e}")
print(f"test accuracy={final.score(X_test, y_test):.3f}")Only the training portion enters the curve computation. The test set stays untouched until the last line.
Limits of the choice
- One hyperparameter at a time. A validation curve varies one setting while holding the others fixed. Settings interact. The best
gammadepends onC, so a curve for one is only valid at the fixed value of the other. - The validation score is optimistic. You picked the value that scored well on those folds, so that score is slightly biased upward. The test score is the honest estimate.
- Flat regions are information. If the curve is flat across several values, the model is insensitive to that setting. Spend your tuning effort elsewhere.
- Curves guide, searches finish. After the curves narrow the plausible range, a grid or randomized search over several hyperparameters can refine the choice. Use nested cross-validation if you need an unbiased estimate of that search.
Re-check after tuning
Tuning one hyperparameter can change the diagnosis. After you pick a value, compute the learning curve again. If the validation curve is still rising at the full training size, more data is likely to help, even though the setting is now well chosen.
Exercise
A validation curve for the regularization strength C shows mean validation accuracy of 0.91 at C=1 (std 0.02) and 0.92 at C=10 (std 0.02). Higher C means weaker regularization. Which value would the one-standard-deviation rule choose, and why?
Show answer
C=1. The two means differ by 0.01, which is smaller than one standard deviation (0.02), so the scores are not distinguishable. C=1 is more strongly regularized and therefore the simpler model.