How Machines Learn Guide 4 of 6 · tap to switch
Overfitting vs Underfitting & Bias-Variance
How to tell real learning apart from blind memorization, understand the bias-variance tradeoff, and cure overfit models.
Picture a small neighborhood cafe trying to predict daily iced-coffee sales from outside temperature. Sales climb quickly as mornings warm up from 8°C to 24°C, then level off above 30°C when people stay indoors. Matching that smooth curve without chasing random daily noise is the core tension of overfitting vs. underfitting and the bias-variance tradeoff.
You only have 11 past days of noisy sales logs (one sunny Tuesday happened to have a street fair; one warm Friday had a sudden shower). Imagine choosing how bendy a wire you use to trace those 11 days. In machine learning, this “allowed curve bends” dial is called the polynomial degree:
- Degree 1 (Straight Line): A stiff straight ruler with zero bends. It underfits, missing the smooth summer curve completely.
- Degree 3 (Smooth Curve): A gentle bendy wire that follows the real rise and summer plateau while ignoring random daily bumps.
- Degree 9 (Rollercoaster): A wild track allowed so many twists that it overfits, zigzagging through every noisy training dot while producing absurd predictions for tomorrow.
Drag the Allowed Curve Bends (Polynomial Degree) slider below from Degree 1 (Straight Line) up to Degree 9 (Rollercoaster), and watch the U-shaped Test Error curve on the right reveal the sweet spot.
Notice the unmistakable fingerprint on the right chart:
- Training error (purple) drops forever as you allow more bends. A model can always look better on past data by memorizing harder.
- Unseen test error (red) drops into a sweet-spot valley around Degree 2 to 3, then climbs sharply back upward as the curve starts chasing random daily noise.
Overfitting vs. Underfitting and the Bias-Variance Tradeoff
Every mistake a model makes on unseen data splits cleanly into two everyday behaviors:
- Bias (Stubborn miss): Even if you retrained the model on ten fresh sets of cafe days, a stiff straight ruler always misses the curved summer peak in the same systematic way.
- Variance (Jitters): A wild degree-9 rollercoaster reacts so dramatically to whichever 11 days you happened to collect that retraining on a different two-week stretch produces a totally different squiggle.
Switch between Too Stiff (Deg 1), Just Right (Deg 3), and Too Jumpy (Deg 9) below and click Show All 10 Samples to watch both the overlaid curves and the dartboard react together.
Every prediction’s expected mistake adds up as Total Error = Bias² + Variance + Irreducible Noise. You can never shrink that third piece (Irreducible Noise, random unrecorded events like a nearby bus breakdown or a pop-up street fair) down to zero. The whole craft of model tuning is finding the sweet spot where Bias² + Variance hits its minimum.
Three real cures when your model is overfitting
Suppose you already have a wildly overfit Degree-9 rollercoaster with huge Unseen Test Error. Why does it spiral so violently between dots? To yank the line sharply up and down from one nearby dot to the next, the model attaches gigantic competing positive and negative multipliers (called weights) that nearly cancel on training days, yet explode everywhere in between.
How do real machine learning teams calm a wild model back down? You have three reliable levers:
- Collect more training examples: Random daily blips average out when the model sees 35 days instead of 11.
- Shrink gigantic weights (Ridge / L2 regularization): Charge the model a gentle penalty whenever its weights grow huge (officially called Ridge or L2 regularization). Because massive competing weights now cost extra, the rollercoaster relaxes into a smooth curve unless the data genuinely demands a bend.
- Simplify the model: Lower its allowed curve bends (degree) or drop noisy input clues.
Start from the wild overfit state below and test each cure one by one. Watch Weight Magnitude shrink dramatically as the weight-shrinking penalty tames the wild swings!
Different model families, same calming idea: Keeping a model from memorizing noise is broadly called regularization. For curve and linear models, you shrink huge weights (L2 regularization). For neural networks, you can randomly switch off a fraction of neurons during each training step so no single pathway memorizes the data (dropout). For decision trees, you cap how many stacked yes/no questions a tree may ask so it cannot isolate single noisy rows (tree depth limit).
Should you collect more data, or build a smarter model?
One of the most expensive mistakes in real-world AI projects is spending six months collecting ten times more training data when the model is actually underfitting.
You can tell in five minutes by plotting Learning Curves (Training Error vs. Validation Error as dataset size grows). Switch between Overfitting Model and Underfitting Model below and drag Training Examples Available from 20 to 500 to read both signatures immediately.
See the Bias-Variance theorem, Ridge regularization & modern double descentThesis / Reference
1. Exact Bias-Variance Decomposition: Suppose true outcomes follow with and . For any estimator trained on random sample , the expected squared prediction error at an unseen test point decomposes exactly into three non-negative terms:
2. Penalized Empirical Risk (Ridge L2 vs. Lasso L1): Regularization constrains effective hypothesis complexity by adding a norm penalty on the slope weights (leaving the intercept unpenalized so baseline shifts remain free):
With Ridge (), the closed-form solution becomes , shrinking correlated weights smoothly toward zero. Replacing with Lasso (, ) drives irrelevant feature weights to exact zero, performing automatic feature selection.
3. Modern Thesis Note (Double Descent): In classical parametric models (linear models, fixed-basis polynomials, shallow trees), test error follows the U-shaped curve shown in Lab 1. In heavily overparameterized deep neural networks, Belkin et al. (2019, PNAS) documented double descent: test error peaks sharply at the interpolation threshold where parameters match sample count, then descends a second time as gradient descent selects minimum-norm interpolating solutions. Even there, validation curves and regularization (weight decay, dropout, data augmentation) remain the primary guardrails.
See Ridge regularization & learning curves in PythonOptional Code
import numpy as np
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import PolynomialFeatures, StandardScaler
from sklearn.linear_model import Ridge
from sklearn.metrics import root_mean_squared_error
# Compare unregularized degree-9 vs Ridge-regularized degree-9
raw_poly = make_pipeline(PolynomialFeatures(9), StandardScaler(), Ridge(alpha=1e-7))
calm_poly = make_pipeline(PolynomialFeatures(9), StandardScaler(), Ridge(alpha=0.5))
raw_poly.fit(X_train, y_train)
calm_poly.fit(X_train, y_train)
print("Overfit Test RMSE:", round(root_mean_squared_error(y_test, raw_poly.predict(X_test)), 2))
print("Ridge Test RMSE:", round(root_mean_squared_error(y_test, calm_poly.predict(X_test)), 2))Quick diagnosis cheat sheet
- Train error high AND Validation error high? You are underfitting. Add richer features, relax regularization, or try a more flexible model. Don’t waste budget collecting more of the same data yet.
- Train error tiny AND Validation error much higher? You are overfitting. Collect more training examples, dial up regularization (weight-shrinking L2, neuron dropout, or a tree depth limit), or simplify the model.
- Not enough rows for a trustworthy single validation curve? Next up in Cross-Validation, you will learn how to rotate every slice of a small dataset through the exam seat for a rock-solid score.
Quick Intuition Check
Four quick real-world scenarios. No scores, just test your intuition.
Your house-price model gets $4k average error on the training set, but $58k average error on unseen test homes. What is happening?
Both your training error (32%) and validation error (34%) are painfully high and stay completely flat even when you double your training data. What should you try next?
How does weight-shrinking with Ridge (L2) regularization calm down a wildly wiggly rollercoaster curve?
With a powerful enough model and enough tuning, you can reduce unseen test error all the way to 0.0 even when real-world measurements contain random unrecorded noise.
Common Questions
What is overfitting in machine learning in simple terms?
What is the difference between overfitting and underfitting?
What is the bias-variance tradeoff?
How do you fix overfitting in practice?
Will adding more training data always fix a bad model?
Related Visual Guides
Batch Normalization, Dropout & Weight Decay
Keep deep neural networks stable and prevent memorization using Dropout, Batch/Layer Normalization, Weight Decay, and Data Augmentation.
Cross-Validation & K-Fold Cross-Validation
How rotating every slice of your dataset through the exam seat turns lucky split roulette into a trustworthy score.
Random Forests
Average hundreds of decorrelated decision trees trained on random bootstrap bags to crush variance and win on tabular datasets out of the box.
Written & illustrated by Muhammad Aashir Irshad · Updated December 2026 · Free open-access reference
Cite this guide in a paper, thesis, or course (BibTeX & APA)
@misc{irshad2026overfittingunderfitting,
author = {Irshad, Muhammad Aashir},
title = {Overfitting vs Underfitting & Bias-Variance: Interactive Visual Guide},
howpublished = {SeeingML},
year = {2026},
month = {dec},
url = {https://seeingml.com/learn/overfitting-underfitting/},
note = {Interactive visual guide}
} Irshad, M. A. (2026). Overfitting vs Underfitting & Bias-Variance: Interactive Visual Guide. SeeingML. https://seeingml.com/learn/overfitting-underfitting/