Sub topic 4
Underfitting: when the model is too simple
This chapter assumes you know supervised learning basics (features, labels, a loss function), have fitted a linear regression, and can use scikit-learn's fit and predict. It also assumes the previous chapters' vocabulary: training error, validation error, and model capacity.
What underfitting is
A model underfits when it is too simple to capture the real pattern in the data. It cannot fit the training set well, so it also does badly on new data. The error is high everywhere.
This differs from overfitting. An overfit model has low training error and high validation error. An underfit model has high training error and a validation error that is close to it.
The usual cause is that the set of functions the model can represent does not contain anything close to the true relationship. A straight line cannot follow a sine curve, however much data you give it or however long you train. In bias-variance terms, underfitting is high bias. The model's systematic assumptions are wrong, so its predictions are off in a consistent direction.
The signature: high error on both sets
To judge "high", you need a reference point. Training error alone does not tell you whether a model underfits, because some problems are noisy and every model has a high error floor.
Useful references are:
- Noise floor. If you know the label noise, the best possible mean squared error is about the noise variance. In the example below the noise standard deviation is 0.2, so the floor is about 0.04.
- Baseline model. Predicting the training mean (regression) or the majority class (classification) gives a minimum bar. A model barely better than this is probably underfitting.
- Human or prior-model performance. If people or an older system do much better, there is room to improve.
- A more flexible model. If a higher-capacity model gets clearly lower training error, the simpler one was limited by capacity.
A worked example
The script below uses the sine data from earlier chapters. It compares a mean-only baseline, a straight line, a degree-4 polynomial, and a degree-4 polynomial with a very strong ridge penalty.
import numpy as np
from sklearn.dummy import DummyRegressor
from sklearn.linear_model import LinearRegression, Ridge
from sklearn.metrics import mean_squared_error
from sklearn.model_selection import train_test_split
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import PolynomialFeatures, StandardScaler
rng = np.random.default_rng(0)
X = rng.uniform(-3, 3, size=(40, 1))
y = np.sin(X).ravel() + rng.normal(0, 0.2, size=40)
X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.3, random_state=0)
models = {
"baseline (mean)": DummyRegressor(strategy="mean"),
"degree 1": make_pipeline(PolynomialFeatures(1), StandardScaler(), LinearRegression()),
"degree 4": make_pipeline(PolynomialFeatures(4), StandardScaler(), LinearRegression()),
"degree 4, ridge alpha=1000": make_pipeline(
PolynomialFeatures(4), StandardScaler(), Ridge(alpha=1000)
),
}
print("noise floor (variance):", 0.2 ** 2)
for name, model in models.items():
model.fit(X_train, y_train)
train_mse = mean_squared_error(y_train, model.predict(X_train))
val_mse = mean_squared_error(y_val, model.predict(X_val))
print(f"{name:28s} train={train_mse:.3f} val={val_mse:.3f}")Run it and compare the rows. The exact numbers depend on the split, but the pattern should be stable:
- The baseline has a large error on both sets.
- The degree-1 line improves on the baseline but stays well above the 0.04 noise floor on both sets. The train and validation errors are close to each other. This is underfitting.
- The degree-4 model gets close to the noise floor on both sets. Its capacity matches the problem.
- The heavily regularised degree-4 model has the same flexibility on paper, but the penalty shrinks its coefficients so far that it behaves like a much simpler model. Its errors move back toward the baseline.
The last row matters in practice. Underfitting is not only about the model family. It can also be caused by settings that restrict a flexible model.
Common causes
| Cause | Example | Typical fix |
|---|---|---|
| Model family too simple | Linear model on a curved relationship | Use a more flexible model, or add nonlinear features |
| Missing or weak features | Predicting house price without location | Engineer or collect informative features |
| Too much regularisation | Ridge `alpha` or lasso `alpha` set very high | Lower the penalty strength |
| Too little training | Neural network stopped after a few epochs | Train longer, check the learning rate |
| Network too small | One hidden layer with 2 units | Add units or layers |
| Over-aggressive early stopping | `patience=1` on noisy validation loss | Increase patience |
Learning curves for underfitting
A learning curve plots training and validation error against the number of training examples. For an underfit model:
- The training error rises quickly as data is added, because a simple model cannot memorise more points.
- The validation error falls and meets the training error.
- Both curves flatten at a high level, with a small gap between them.
The flat, converged shape tells you the model has already extracted what it can. Collecting more data will not help. The error stays at the plateau because the model's assumptions are the limit.
Fixing underfitting
Work in this order, from cheapest and most informative to more costly:
- Check for a bug. A wrong target column, unscaled inputs for a gradient-based model, or a learning rate that is too small can all look like underfitting. Confirm that the model can overfit a tiny subset of 10 to 20 examples. If it cannot, the problem is likely a bug or an optimisation issue, not capacity.
- Reduce regularisation. If a penalty is active, lower it and see whether training error drops.
- Add features. Polynomial terms, interactions, or domain-specific features give a simple model more to work with.
- Increase capacity. Move to a more flexible model family, or enlarge the network.
- Train longer. For iterative models, check that the training loss has stopped decreasing, not just that you ran out of epochs.
Each step raises capacity, so watch the validation error as you go. When training error approaches the reference level and the gap to validation error begins to grow, you are leaving the underfitting regime and approaching overfitting.
Edge cases
- Noisy labels. If labels are very noisy, the error floor is high. A model can be perfect and still show a large error. Compare against the noise floor or a strong baseline, not against zero.
- Imbalanced classes. Accuracy can look high while the model has learned nothing beyond the majority class. Check against a majority-class baseline and use metrics such as recall or F1.
- Underfitting in one region. Overall error can look fine while the model underfits a subgroup or range of inputs. Inspect residuals by segment.
- Optimisation failure. A deep network with a bad initialisation or learning rate can sit at high loss. This resembles underfitting, but the fix is in the training setup, not the architecture.
Exercise
A model has a training MSE of 0.92 and a validation MSE of 0.95. A mean-only baseline scores 1.00 on the same validation data. Does the model underfit or overfit, and which two changes would you try first? Would collecting ten times more data help much?
Show answer
It underfits. Training error is high and close to validation error, and the model is barely better than the baseline. First check for a bug (can the model overfit a small subset?), then try the cheapest capacity changes: lower any regularisation strength and add informative or nonlinear features, or use a more flexible model. Ten times more data would not help much, because the learning curves have already converged at a high error. The limit is the model, not the amount of data.