Sub topic 11
From Diagnosis to Action
This chapter assumes you can compute learning curves and validation curves with scikit-learn and read the gap between training and validation scores. Here you turn a diagnosis into a concrete next step.
Start with the symptom
A curve gives you two numbers to compare: the training score and the validation score. Together they point to one of three situations.
| Symptom | Diagnosis | Typical remedies |
|---|---|---|
| Both scores are low and close together | High bias (underfitting) | More flexible model, better features, less regularization |
| Training score is high, validation score is much lower | High variance (overfitting) | More data, simpler model, more regularization, fewer features |
| Both scores are high and close together | Good fit | Stop tuning, or check whether the metric is the right one |
"Low" and "high" are relative to a target. Decide what score you need, or what a simple baseline achieves, before you read the curves.
Decision flow
Remedies for high bias
When both curves plateau at a low score, more data will not help. The model cannot represent the pattern. Try these:
- Use a more flexible model, such as a deeper tree or a non-linear kernel.
- Add informative features, such as interaction terms.
- Reduce regularization, for example by raising
CinLogisticRegressionorSVC.
Remedies for high variance
When training is near perfect and validation lags behind, the model has memorized noise. Look at the learning curve first. If the validation curve is still rising at the largest training size, more data should help. If it has flattened, change the model instead:
- Increase regularization, for example by lowering
C. - Limit capacity, such as
max_depthfor trees orgammafor an RBF kernel. - Remove noisy or redundant features.
Worked example
Suppose an RBF SVC on the digits data shows a training score of 1.00 and a validation score of 0.85 at a large gamma. The next snippet compares two settings so you can see the effect of a remedy.
from sklearn.datasets import load_digits
from sklearn.model_selection import cross_val_score
from sklearn.svm import SVC
X, y = load_digits(return_X_y=True)
for gamma in (1e-1, 1e-3):
scores = cross_val_score(SVC(kernel="rbf", gamma=gamma), X, y, cv=5)
print(f"gamma={gamma:.0e} validation={scores.mean():.3f} +/- {scores.std():.3f}")The large gamma makes the decision boundary very flexible, so it overfits. Lowering it is a remedy for variance. Rerun the validation curve after any change, because a fix for one problem can create the other.
Check the size of the effect
Before acting, compare the gap with the fold-to-fold spread. A difference smaller than one standard deviation across folds may be noise. Make one change at a time, so you know which change caused a shift in the curve.
Exercise
Exercise
A learning curve shows a training accuracy of 0.62 and a validation accuracy of 0.60, and both lines are flat across all training sizes. What is the diagnosis, and which remedy would you try first: collecting more data, or increasing model capacity?
Show answer
Both scores are low and close together, so this is high bias (underfitting). Collecting more data will not help because the curves are already flat. Increase model capacity first, for example by using a more flexible model, adding features, or reducing regularization.
Exercise
A model has a training score of 0.99 and a validation score of 0.80. The validation curve is still rising at the largest training size. What do you try first?
Show answer
This is high variance, and the rising validation curve suggests the model can still benefit from more data. Collect more training data first. If that is not possible, increase regularization or reduce model complexity.