Bias–Variance Trade-off Explained: Underfitting vs Overfitting

| | 9 min read

Every machine learning model makes mistakes on data it hasn’t seen. The bias–variance trade-off explains where those mistakes come from, and it’s the most useful idea I know for deciding what to do next when a model disappoints: make it more flexible, or rein it in.

This guide explains bias and variance in plain terms, shows how they lead to underfitting and overfitting, walks through a small scikit-learn experiment you can run yourself, and ends with a practical checklist for diagnosing and fixing each problem.

Where prediction error comes from

Imagine training the same kind of model many times, each time on a fresh sample of data from the same source, and then asking each version to predict the same new point. For a regression model judged by squared error, the average error you’d see splits neatly into three parts:

Expected test error = Bias² + Variance + Irreducible noise

  • Bias is how far the average prediction is from the truth. It comes from the model’s built-in assumptions being too simple for the real pattern.
  • Variance is how much the predictions move around from one training sample to the next. It comes from the model reacting to quirks of the particular data it saw.
  • Irreducible noise is randomness in the data itself, such as measurement error or factors you didn’t record. No model can remove it, so it sets a floor on the error you can reach.

You can’t measure these terms directly on a real project, because you only have one training set. But the split tells you which levers exist: you can lower bias, lower variance, or accept that you’ve hit the noise floor.

Bias: when a model is consistently wrong

Some models are too simple to capture the relationships that matter. Fit a straight line to data that follows a curve and the line will sit below the curve in some places and above it in others, all the way along. Its predictions aren’t randomly off; they’re consistently off in the same regions. That systematic error is bias.

A high-bias model underfits. The tell-tale sign is that it performs poorly even on the data it was trained on. More training data won’t help much, because the model simply can’t represent the true shape.

Variance: when a model is unstable

Other models are so flexible that, while learning the real relationships, they also learn coincidences: patterns that exist only in this particular training sample. Change the training data slightly and the model’s predictions can change a lot. Its errors aren’t consistent; they swing depending on what it happened to see. That sensitivity is variance.

A high-variance model overfits. Its training error looks impressive, sometimes close to zero, but its error on new data is much higher. The gap between the two is the warning sign.

A simple way to remember both: think of teaching a child to recognise dogs. Show them only small dogs and they won’t recognise a Great Dane, because their idea of “dog” is too narrow (underfitting). Show them hundreds of photos where every dog happens to wear a costume and they may decide the costume is what makes a dog, then fail on a plain one (overfitting).

Underfitting vs overfitting at a glance

Underfitting Overfitting
Main source of error High bias High variance
Model complexity Too simple for the real pattern Complex enough to fit the noise too
Training error High Low, often very low
Validation error High, close to training error Much higher than training error
Does more data help? Rarely Usually
Typical causes Too few or weak features, overly strong regularisation, a model that’s too restrictive Too many parameters for the data, noisy features, too little regularisation, training for too long

Why it’s called a trade-off

As you make a model more flexible (more parameters, deeper trees, higher-degree polynomials, weaker regularisation), bias tends to fall and variance tends to rise. Test error usually traces a U shape: it falls at first as the model starts capturing the real pattern, reaches a minimum, then climbs again as variance takes over.

It’s easy to get one of the two low on its own. A curve that passes exactly through every training point has almost no bias but huge variance. A flat line that ignores the inputs has no variance at all but huge bias. The hard part is finding a model where both are low enough.

Where that sweet spot sits depends on the problem, not just the algorithm:

  • If the true relationship is close to linear, a simple model already has low bias. Extra flexibility buys almost nothing and quickly adds variance, so the best model is a simple one.
  • If the true relationship is strongly non-linear, simple models carry a lot of bias. Extra flexibility cuts error sharply before variance starts to matter, so the best model is a more flexible one.
  • The amount of data matters too. With more examples, a flexible model has less room to latch on to coincidences, so its variance shrinks and the sweet spot moves towards more complex models.

That’s why there’s no universally “right” model complexity. You find it by measuring.

See it in code

Here’s a small experiment. We generate 30 noisy points from a sine curve, then fit polynomial regression models of degree 1 (a straight line), 4 and 15, using 5-fold cross-validation to compare training error with validation error.

import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import cross_validate
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import PolynomialFeatures

rng = np.random.default_rng(42)

# A curved relationship plus noise: y = sin(2*pi*x) + noise
X = rng.uniform(0, 1, size=(30, 1))
y = np.sin(2 * np.pi * X).ravel() + rng.normal(0, 0.2, size=30)

for degree in [1, 4, 15]:
    model = make_pipeline(PolynomialFeatures(degree), LinearRegression())
    scores = cross_validate(
        model, X, y,
        cv=5,
        scoring="neg_mean_squared_error",
        return_train_score=True,
    )
    train_mse = -scores["train_score"].mean()
    valid_mse = -scores["test_score"].mean()
    print(f"degree {degree:>2}: train MSE {train_mse:.3f}, validation MSE {valid_mse:.3f}")

Running it with scikit-learn gives:

degree  1: train MSE 0.182, validation MSE 0.192
degree  4: train MSE 0.028, validation MSE 0.054
degree 15: train MSE 0.017, validation MSE 0.063
  • Degree 1 underfits. Training and validation error are both high and close together: the line simply can’t follow a sine wave. That’s bias.
  • Degree 15 overfits. It has the lowest training error of the three, yet its validation error is almost four times higher, and worse than degree 4. It’s bending to fit the noise. That’s variance.
  • Degree 4 is the best balance here: low training error and the lowest validation error.

Notice that training error alone would have picked degree 15, the worst choice for new data. Always judge a model on data it didn’t train on.

How to diagnose bias and variance on a real project

Compare training error with validation error (or cross-validated error), and if you can, compare both with a sensible baseline such as human-level performance or a simple heuristic.

What you see Likely problem
Training error high, validation error about the same High bias (underfitting)
Training error low, validation error much higher High variance (overfitting)
Both high, with a big gap between them Both at once: fix bias first, then variance
Both low and close together Good fit, possibly near the noise floor

Learning curves make this clearer. Plot training and validation error as you train on more and more data (scikit-learn’s learning_curve does this). If the two curves meet at a high error, more data won’t help: you need a more capable model. If there’s a wide gap that narrows as data grows, collecting more data is likely to pay off.

How to fix high bias

  • Use a more flexible model: more layers or units, deeper trees, or non-linear terms.
  • Add better features. Often a single well-chosen feature does more than a bigger model.
  • Reduce regularisation if it’s holding the model back.
  • Train for longer, if training stopped before the model converged.
  • Try boosting, which builds a strong model from a series of weak ones and mainly reduces bias.

How to fix high variance

  • Get more training data, or use data augmentation where that makes sense.
  • Add or strengthen regularisation: L2 (ridge), L1 (lasso), dropout or weight decay.
  • Simplify the model: fewer features, shallower trees, a lower polynomial degree.
  • Stop training early, when validation error starts rising.
  • Use bagging, such as a random forest, which averages many models trained on different samples and mainly reduces variance.

Bagging and boosting are covered in more detail in my guide to ensemble methods.

Does this apply to classification too?

Yes, with one caveat. First, the difference between the two problem types:

  • Regression predicts a continuous quantity, such as a price or a temperature. It’s usually measured with mean squared error (MSE), root mean squared error (RMSE) or mean absolute error.
  • Classification predicts a discrete label, such as spam or not spam. It’s usually measured with accuracy, precision and recall, F1 or log loss. Many classifiers output a probability for each class, so they predict a continuous number under the hood.

Some algorithms, such as decision trees and neural networks, handle both kinds of problem with small changes. Others are built for one: linear regression for regression, logistic regression for classification, despite its name.

The tidy formula above, Bias² + Variance + Noise, holds exactly only for squared error. For classification metrics such as accuracy, the maths is messier. But the practical reasoning carries over unchanged: a classifier that does badly on its own training data is too simple, and one that aces training but stumbles on validation data is too sensitive to its training sample. The same diagnosis table and the same fixes apply.

Key takeaways

  • Bias is systematic error from a model that’s too simple; variance is instability from a model that’s too sensitive to its training data.
  • High bias means underfitting; high variance means overfitting.
  • More flexibility lowers bias and raises variance, so test error is lowest somewhere in between, and where that is depends on your data.
  • Diagnose by comparing training and validation error, ideally with learning curves, never training error alone.
  • Fix bias with more capacity and better features; fix variance with more data, regularisation and simpler models.

Further reading

The post Bias–Variance Trade-off Explained: Underfitting vs Overfitting appeared first on Alpesh Kumar.

Subscribe to Our Newsletter

We don’t spam! Read our privacy policy for more info.