Sub topic 4
Underfitting: when the model is too simple
This chapter assumes you know supervised learning basics, how a loss function is minimised, and scikit-learn's fit and predict. It also assumes you have met the train/validation split from the previous chapters.
What underfitting is
A model underfits when it cannot capture the real pattern in the data. Its capacity is too low. Capacity is the range of functions the model can represent. A straight line has low capacity. It cannot follow a curve, however much data you give it or however long you train.
The defining symptom is high error on the training set. Because the model cannot even fit the data it has seen, it also does poorly on validation data. Underfitting is also called high bias, because the model's built-in assumptions (here, "the relationship is a straight line") are wrong for the data.
Recognising it from error values
Compare training and validation error against a sensible baseline, such as the noise level or a simple benchmark.
| Training error | Validation error | Likely diagnosis |
|---|---|---|
| High | High, close to training error | Underfitting |
| Low | Much higher than training error | Overfitting |
| Low | Low, close to training error | Good fit |
The key signal is that both errors are high and close together. A small gap does not mean the model is good. It may simply be equally bad everywhere.
A worked example
The data below follows a sine curve with noise. A degree-1 polynomial is a straight line, so it cannot follow the curve.
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import PolynomialFeatures, StandardScaler
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error
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)
for degree in (1, 4):
model = make_pipeline(PolynomialFeatures(degree), StandardScaler(), LinearRegression())
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(degree, round(train_mse, 3), round(val_mse, 3))The noise has standard deviation 0.2, so the best achievable mean squared error is near 0.04. Run the code and compare each row to that figure. The degree-1 model should sit well above it on both sets. The degree-4 model should land much closer to it.
Fixing underfitting
Underfitting means the model lacks flexibility, so the fixes add flexibility or information:
- Use a more expressive model, such as a higher polynomial degree, a deeper tree, or a larger network.
- Add informative features or interaction terms.
- Reduce regularisation, because a strong penalty can force a model to be too simple.
- Train longer, if the optimiser has not yet converged.
Exercise
A model has training MSE 0.9 and validation MSE 0.95, while the noise level is about 0.04. Is it underfitting or overfitting, and what is one sensible next step?
Show answer
It is underfitting. Both errors are high and close together, far above the noise level. A sensible next step is to increase capacity, for example by using a higher polynomial degree or adding features, or to reduce the regularisation strength.