Skip to content
Machine Learning
Regression 9 min read

The Bias–Variance Tradeoff

Why models fail in two opposite ways — and how to diagnose which one is happening to you, with validation curves, learning curves, and a cheat sheet.

Download notebook Open Google ColabIn Colab: File → Upload notebook → pick the downloaded file.

In the last lesson you watched test error trace a U as model complexity grew. The bias–variance tradeoff is the theory behind that U — and more usefully, it's a diagnostic framework: models fail in two opposite ways, each with its own symptoms and its own cures. Prescribing the wrong cure (adding data to a high-bias model, adding complexity to a high-variance one) wastes weeks. This lesson teaches you to tell the two apart.

Three ingredients of prediction error

The expected error of a model on new data decomposes, conceptually, into three parts:

expected error = bias² + variance + irreducible noise

  • Bias — error from wrong assumptions. A straight line fitted to a curve is biased: no matter how much data you give it, it systematically misses the shape. High bias = underfitting.
  • Variance — error from sensitivity to the training sample. A flexible model fitted to 40 noisy points would look completely different if you drew 40 different points. High variance = overfitting.
  • Irreducible noise — randomness in the data itself (measurement error, unmodeled factors). No model can remove it; it's the floor under your error.

The tradeoff: making a model more flexible reduces bias but raises variance, and vice versa. The best model balances the two — the bottom of the U.

Seeing variance with your own eyes

"Variance" sounds abstract until you watch it. Below we draw several bootstrap samples (resamples of the same dataset) and fit the same model to each. A degree-2 fit barely notices which sample it got; a degree-9 fit changes shape completely every time:

Python — runs in your browser

Left: six nearly identical curves — low variance (but they all miss the sine wiggle the same way: that consistent miss is bias). Right: six wildly different curves — each one chased the noise of its particular sample. That disagreement is variance, and it's why the degree-9 model's test error is so bad: on average, a randomly-drawn wiggly curve is far from the truth.

Symptoms and cures

High bias (underfitting) looks like:

  • Training error is high — the model can't even fit the data it has seen.
  • Test error is about equally high; the train/test gap is small.
  • More data doesn't help — the curves just confirm the same wrong shape.

Fixes: add complexity — more/better features (polynomial terms, interactions, domain-driven features like the taxi distance you saw), a more flexible model family, or less regularization.

High variance (overfitting) looks like:

  • Training error is very low, sometimes near zero.
  • Test error is much higher — a big train/test gap.
  • Results change a lot between random seeds or resamples.

Fixes: constrain or stabilize — more training data, a simpler model (lower degree), regularization (next lessons), or averaging many models (ensembles, later in the course).

This logic drives hyperparameter tuning

Almost every hyperparameter is a complexity dial you can reason about. KNN: increasing n_neighbors averages over more points → less complexity → fights overfitting. Random forests: increasing max_depth adds decisions → more complexity → risks overfitting, while increasing n_estimators averages more trees → less variance. When a model overfits, ask: which dial turns complexity down?

Validation curves: error vs complexity

scikit-learn automates the "U-curve" experiment with validation_curve: it sweeps one hyperparameter and cross-validates at each value. Here we sweep KNN's n_neighbors (note: for KNN, small n_neighbors = high complexity, so the x-axis runs complex → simple):

Python — runs in your browser

Read it like a doctor: on the left (k = 1), train R² is perfect while validation lags — variance zone. On the far right (k = 80, nearly averaging everything), both scores collapse together — bias zone. The sweet spot is where validation peaks.

Learning curves: will more data help?

The second diagnostic sweeps training set size instead. Its shape answers the most expensive question in ML — "should we collect more data?":

Python — runs in your browser

With little data the flexible model aces training but flops on validation (variance). As data grows, the two curves converge — more data is shrinking the variance. Two endgames to recognize:

  • Curves still converging with a gap → more data will help.
  • Curves already converged at a mediocre score → the model has hit its bias floor; more data won't help — you need a better model or features.

Diagnosis cheat sheet

ObservationDiagnosisTry
Train error high, test error similarly highHigh bias (underfit)More features, higher degree, more flexible model, less regularization
Train error tiny, test error much worseHigh variance (overfit)More data, simpler model, regularization, ensembling
Learning curves converged, both mediocreBias floor reachedBetter features / model family — more data is wasted money
Learning curves still converging with a gapVariance, curableCollect more data
Fits change wildly across random seedsHigh varianceSame as overfitting fixes
Test error can't go below some level no matter whatIrreducible noiseAccept it, or measure better data

Check your understanding

5 questions · free
  1. Q1.A model has high training error AND high test error, with almost no gap between them. What's the diagnosis?

  2. Q2.In the bootstrap demo, the six degree-9 curves disagreed wildly with each other. That disagreement is a direct picture of…

  3. Q3.Your learning curve shows train and validation scores that have already converged — to a disappointing R² of 0.45. What's the best next step?

  4. Q4.Your KNN regressor is overfitting. Based on complexity reasoning, which change fights it?

  5. Q5.Even a perfect model can't reach zero test error on real data. Why?

Exercise: Diagnose a tree with a validation curve

Decision trees (coming later in the course) have a complexity dial called max_depth. Using the same synthetic sine data as above, run validation_curve on a DecisionTreeRegressor with max_depth from 1 to 12 and plot train vs validation R². Identify the bias zone, the variance zone, and the best depth. Then answer in a comment: does increasing max_depth fight overfitting or cause it?

Next: a quiet culprit behind unstable models — features on wildly different scales, and the transforms that fix them.