Sub topic 9
Reading a Validation Curve
This chapter assumes you know how to compute a validation curve with validation_curve, and that you understand training versus validation scores, overfitting, and cross-validation. Here you learn to read the curve and pick a hyperparameter value from it.
What the curve shows
A validation curve plots the mean training score and the mean cross-validated score against one hyperparameter. Everything else stays fixed. The x-axis is a knob that changes model flexibility, so the curve shows how fit changes as the model gets more or less complex.
Run this to get the numbers for an RBF support vector classifier. The parameter gamma controls how far one training point influences its neighbors. A larger gamma makes the decision boundary more flexible.
import numpy as np
from sklearn.datasets import load_digits
from sklearn.model_selection import validation_curve
from sklearn.svm import SVC
X, y = load_digits(return_X_y=True)
param_range = np.logspace(-6, -1, 6)
train_scores, val_scores = validation_curve(
SVC(kernel="rbf"),
X,
y,
param_name="gamma",
param_range=param_range,
cv=5,
scoring="accuracy",
)
for gamma, tr, va in zip(param_range, train_scores.mean(axis=1), val_scores.mean(axis=1)):
print(f"gamma={gamma:.0e} train={tr:.3f} validation={va:.3f}")Three regions
A typical curve has three regions, read from left to right when the knob increases flexibility.
- Underfitting region. Training and validation scores are both low and close together. The model is too simple to capture the pattern. This is high bias.
- Good region. Both scores are high. The validation score reaches its maximum, and the gap is small.
- Overfitting region. The training score stays high or keeps rising while the validation score drops. The widening gap is high variance.
For gamma on the digits data, the smallest values give low scores on both sides. Around 1e-3 the validation score peaks. At 1e-1 the training score stays high while validation accuracy collapses.
The direction flips for hyperparameters that control regularization. A larger C in SVC or a smaller alpha in Ridge makes the model more flexible. Check which way each knob points before you label a region.
Choosing a value
Pick the value where the validation score is highest. Then check three things:
- The gap. A large gap at the peak means the model is still overfitting somewhat. A different knob, such as more data, may help more.
- The spread. If the peak sits inside the fold-to-fold standard deviation of its neighbors, the values are statistically hard to tell apart. Prefer the simpler setting, which here is the lower-flexibility one.
- The edges. If the best value is at the end of your range, the true optimum may lie outside it. Extend the range and rerun.
Limits of the method
A validation curve varies one hyperparameter while the others stay fixed. The best gamma can depend on C, so the peak you find is only the best value for that fixed C. For interacting parameters, use a grid or random search, and use the curve afterward to understand the result.
Use a log-spaced range for scale-type parameters such as gamma, C, or alpha. A linear range would waste most points at one end.
Exercise
A validation curve for a decision tree's max_depth shows training accuracy of 1.00 at every depth above 10. Validation accuracy peaks at depth 6 with 0.86, then falls to 0.78 at depth 20. Which depth should you pick, and why? What does the region above depth 6 indicate?
Show answer
Pick a depth near 6, where validation accuracy peaks. Above depth 6 the model is overfitting: training accuracy stays at 1.00 while validation accuracy falls, so the gap widens. If depths 5 and 6 are within one standard deviation of each other across folds, prefer 5, the simpler model.