Sub topic 18
A practical workflow for diagnosing and fixing a model
This chapter assumes you know train, validation and test splits, k-fold cross-validation, and the ridge, lasso, dropout and early stopping techniques from earlier chapters. Here you combine them into one repeatable routine.
Start with a baseline and the right numbers
Split off a test set first and leave it alone. Then fit a simple baseline and record two numbers: the training error and the cross-validated error. Every later decision compares these two, so compute them the same way each time.
Diagnose before you fix
Compare the errors with each other and with a target level. The target might be human performance, a business requirement, or the noise level of the data.
| Training error | Validation error | Diagnosis | Typical remedies |
|---|---|---|---|
| High | High, close to training | High bias (underfitting) | More capacity, better features, weaker regularisation, longer training |
| Low | Much higher than training | High variance (overfitting) | More data, augmentation, stronger regularisation, dropout, early stopping, simpler model |
| Low | Low, close to training | Good fit | Evaluate once on the test set |
If both errors are high and a larger model does not help, check the data. Label noise and leakage both produce confusing results.
Use a learning curve to decide between data and capacity
A learning curve shows error as the training set grows. If the validation error is still falling and a gap remains, more data should help. If both curves have flattened at a high error, more data will not help, and you need a more flexible model.
import numpy as np
from sklearn.model_selection import learning_curve
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import PolynomialFeatures, StandardScaler
from sklearn.linear_model import Ridge
rng = np.random.default_rng(0)
X = rng.uniform(-3, 3, size=(200, 1))
y = np.sin(X).ravel() + rng.normal(0, 0.2, size=200)
model = make_pipeline(PolynomialFeatures(15), StandardScaler(), Ridge(alpha=1e-4))
sizes, train_scores, val_scores = learning_curve(
model, X, y, cv=5, scoring="neg_mean_squared_error",
train_sizes=np.linspace(0.2, 1.0, 5), shuffle=True, random_state=0,
)
for n, tr, va in zip(sizes, -train_scores.mean(axis=1), -val_scores.mean(axis=1)):
print(n, round(tr, 3), round(va, 3))Because the preprocessing sits inside the pipeline, each fold fits its scaler on its own training data only. This keeps the curve free of leakage.
Change one thing at a time
Apply a single remedy, then re-measure with the same cross-validation setup. Tune the regularisation strength on the validation folds, never on the test set. If you change several things at once, you cannot tell which one helped.
Stop when the validation error is close to your target, or when further changes improve it by less than the fold-to-fold variation. At that point, evaluate on the test set once and report that number.
Common mistakes
- Fixing overfitting with more regularisation when the real problem is high bias.
- Fitting the scaler on the full dataset before splitting.
- Reading a small validation gap as meaningful without checking the spread across folds.
Exercise
A model has a training MSE of 0.80 and a cross-validated MSE of 0.85. The target MSE is 0.20. Which remedy should you try first: stronger L2 regularisation, or adding polynomial features? Explain.
Show answer
Add polynomial features (more capacity). Both errors are high and close together, so the model has high bias. Stronger regularisation would reduce capacity further and make the fit worse.