Blog

Bias vs Variance: The Bias-Variance Tradeoff Explained with Code

October 12, 2026 · Pushpjeet Cholkar

Last updated:

Short answer: Bias is error from a model that is too simple, so it misses the real pattern (underfitting). Variance is error from a model that is too sensitive to its training data, so it learns noise (overfitting). Total error is bias² plus variance plus irreducible noise; you tune model complexity to minimize the sum on validation data.

Prefer to see it first? The interactive bias-variance video and practice games explain the same idea in 74 seconds with “too cold, too hot, just right” scenes.

Bias vs variance at a glance

High bias High variance
Plain meaning Model too simple; misses the real pattern Model too sensitive; learns noise in the training set
Also called Underfitting Overfitting
Training error High Low
Validation / test error High, close to training error Much higher than training error
Changes if you retrain on a new sample? Little; wrong in the same way each time A lot; predictions swing with the sample
Typical cause Too few features, model too rigid, too much regularization Too many parameters for the data, too little data, too little regularization
Helps More features, more flexible model, less regularization, train longer More data, regularization, simpler model, feature selection, early stopping, ensembles like bagging
Doesn’t help much Adding more rows of the same data Adding more complexity

What is bias in machine learning?

Bias is the gap between what your model predicts on average and the true value. Imagine training the same kind of model many times on different samples from the same source. If the average of all those models is still off, that systematic error is bias.

A straight line fit to a curved relationship is the classic example. It doesn’t matter how many samples you give it: a line can’t bend, so it is wrong in the same places every time. That is underfitting.

Note: this is statistical bias. It is different from fairness bias (a model treating groups unequally) and from the “bias” term (intercept) inside a neural network layer.

What is variance in machine learning?

Variance measures how much the model’s predictions change when you train it on a different sample. A very flexible model, such as a high-degree polynomial or a deep decision tree, can bend to pass through every training point, including the random noise. Train it on another sample and you get a quite different curve.

That model looks great on training data and disappoints on new data. That is overfitting.

What is the bias-variance tradeoff?

For squared-error loss, the expected prediction error at a point splits into three parts:

Expected error = Bias² + Variance + Irreducible noise

As you make a model more flexible, bias usually falls and variance usually rises. Total validation error therefore tends to follow a U shape: high on the simple end (bias), high on the complex end (variance), lowest somewhere in between. The “tradeoff” is choosing complexity, features and regularization to sit near the bottom of that U. The Elements of Statistical Learning (Hastie, Tibshirani, Friedman) covers the derivation in detail.

How to tell if your model has high bias or high variance

You don’t need the formula to diagnose a real model. You need two numbers: training score and validation score.

What you see Likely problem First thing to try
Training error high, validation error high, gap small High bias More informative features or a more flexible model
Training error low, validation error much higher High variance More data or stronger regularization
Both errors low, gap small Good fit Confirm on a held-out test set, then stop tuning
Both errors high, gap large Both problems, or a data issue Check for leakage, label errors and train/validation mismatch first

A learning curve (score vs training-set size) makes this clearer. With high bias, both curves flatten out early at a poor score and adding data does little. With high variance, there is a wide gap that narrows as data grows. scikit-learn’s learning_curve and validation_curve utilities plot both. Post 07 in this series covers learning curves in depth.

Python demo: measuring bias and variance

Bias and variance are defined over many possible training sets, so you can only measure them directly in a simulation where you know the true function. This script does exactly that with NumPy:

"""Bias-variance decomposition demo (NumPy only).
Fits polynomials of degree 1, 4 and 8 to 200 noisy training sets drawn
from the same true function, then measures bias^2, variance and test error
at fixed test points. Run: python3 bias_variance_demo.py
"""
import numpy as np

rng = np.random.default_rng(42)
NOISE_SD = 0.3
N_TRAIN, N_SETS = 30, 200
x_test = np.linspace(0.05, 0.95, 50)  # stay inside the training range

def true_f(x):
    return np.sin(2 * np.pi * x)

y_true = true_f(x_test)
print(f"{'degree':>6} {'bias^2':>8} {'variance':>9} {'noise':>6} {'expected MSE':>13} {'train MSE':>10}")
for degree in (1, 4, 8):
    preds = np.empty((N_SETS, x_test.size))
    train_mse = []
    for i in range(N_SETS):
        x = rng.uniform(0, 1, N_TRAIN)
        y = true_f(x) + rng.normal(0, NOISE_SD, N_TRAIN)
        model = np.polynomial.Polynomial.fit(x, y, degree)
        preds[i] = model(x_test)
        train_mse.append(np.mean((model(x) - y) ** 2))
    mean_pred = preds.mean(axis=0)
    bias2 = np.mean((mean_pred - y_true) ** 2)
    variance = np.mean(preds.var(axis=0))
    noise = NOISE_SD ** 2
    print(f"{degree:>6} {bias2:>8.3f} {variance:>9.3f} {noise:>6.3f} {bias2 + variance + noise:>13.3f} {np.mean(train_mse):>10.3f}")

Output (Python 3.13.5, NumPy 2.5.3, run on October 12, 2026):

degree   bias^2  variance  noise  expected MSE  train MSE
     1    0.154     0.022  0.090         0.266      0.264
     4    0.003     0.018  0.090         0.111      0.078
     8    0.004     0.442  0.090         0.536      0.063

How to read it:

Two lessons sit in those numbers. First, training error alone keeps falling as you add complexity, so it can’t pick your model. Second, no model got below the 0.090 noise line, which is exactly what the formula predicts. (If you push the degree higher or test outside the training range, variance grows explosively; that is why the test points stay inside it.)

How to fix high bias

How to fix high variance

Common mistakes

Frequently asked questions

What is the difference between bias and variance?

Bias is error from a model that is too simple to capture the real pattern, so it is wrong in the same way on every dataset. Variance is error from a model that is too sensitive to its training data, so its predictions change a lot when the training sample changes.

Is high bias underfitting and high variance overfitting?

Yes, in practice. High bias shows up as underfitting: training and validation errors are both high and close together. High variance shows up as overfitting: training error is low but validation error is much higher.

How do I tell if my model has high bias or high variance?

Compare training and validation scores. Both poor and close together points to high bias. Training much better than validation points to high variance. A learning curve, which plots both scores as the training set grows, makes the pattern easier to read.

Does more training data fix high bias?

Usually not. A model that is too simple stays too simple with more rows. More data mainly helps high variance. For high bias, add useful features, use a more flexible model or reduce regularization.

Can you have low bias and low variance at the same time?

You can get both low enough for the task, but not zero. Irreducible noise sets a floor on error, and for a fixed amount of data, making a model more flexible usually trades bias for variance. The goal is the lowest total validation error.

Does the bias-variance tradeoff apply to deep learning?

The idea of diagnosing errors with training and validation scores still applies. Very large neural networks can behave differently from the classic U-shaped curve, a pattern researchers call double descent, so treat the tradeoff as a diagnostic tool rather than a strict law.

Sources

All checked on October 12, 2026.


Learn machine learning with guided practice. New to ML? Start with AI/ML fundamentals and the what to learn in AI/ML roadmap. For live instructor-led classes, see the live AWS and AI/ML training page. If you are in the United States and want online AWS and AI/ML training in the USA, that page covers the US offering. Working with LLMs next? Read RAG vs fine-tuning.

More machine learning guides and tutorials: Machine learning tutorials and guides.

Newsletter

Enjoyed this post?

Get new posts, AI/ML tutorials, AWS batch dates and Oracle tips by email. No spam, unsubscribe anytime.

I’m interested in

Double opt-in: you’ll get a confirmation email first. Privacy policy

WhatsApp