Lesson 02
Bias and Variance — Beginner Guide to Underfitting & Overfitting
Learn bias, variance, underfitting, overfitting, and generalization step by step—starting with ML models, prediction error, and study-hours examples.
What You Will Learn
Click a topic to jump to that section.
- What is a Machine Learning model?
- What is prediction error?
- Bias
- Good fit — low bias
- Variance
- Underfitting and overfitting
- Generalization
- Summary
What is a Machine Learning model?
A machine learning model is a recipe that turns inputs into predictions. On this page:
- Input feature X = study hours
- Target Y = exam marks
- Training examples = rows you show the model while it fits (learns)
- Prediction Ŷ (“Y-hat”) = the mark the model outputs for a given X
Pipeline in plain English: past data → fit → predict new hours.
We use one small table for the whole lesson:
| Study hours (feature X) | Exam marks (target Y) |
|---|---|
| 1 | 35 |
| 2 | 40 |
| 3 | 50 |
| 4 | 55 |
| 5 | 65 |
Note: marks also depend on sleep, health, and prior knowledge. This table is illustrative—it teaches bias and variance, not a full model of exam scores.
What is prediction error?
For one student, error is how far the prediction is from the truth:
error = Y − Ŷ
If the model predicts 52 marks but the student scored 55, error = 55 − 52 = 3 marks.
We often square errors and average them so large mistakes count more—that is mean squared error (MSE). Small average error on new students means good generalization.
Bias
Bias is systematic error: the model’s assumptions are too simple, so it misses the real pattern in your table.
Using our training rows, marks clearly rise as study hours go up (35 → 65). A high-bias model might ignore that trend and predict the same mark for everyone.
The simplest constant guess is the average mark in the table:
Ŷ = (35 + 40 + 50 + 55 + 65) ÷ 5 = 49
That is what the blue line in the canvas shows (y = 49). It is not a random round number like “about 48”—it is the mean of your Y column (the best constant prediction if you minimize squared error).
| Study hours (X) | Actual marks (Y) | High-bias guess (Ŷ = 49) | Error (Y − Ŷ) |
|---|---|---|---|
| 1 | 35 | 49 | −14 |
| 2 | 40 | 49 | −9 |
| 3 | 50 | 49 | +1 |
| 4 | 55 | 49 | +6 |
| 5 | 65 | 49 | +16 |
The errors do not cancel out—they show the model is consistently wrong in a predictable way. That is bias. In ML we call this underfitting: the model is too simple for the data.
Example 1 — canvas (high bias)
Interactive pattern canvas: flat blue Ŷ, pink bias arrows, faded green trend.
Loading canvas…
Try it: change hours or marks, hide a point with the eye icon, or add rows—the blue line stays at the average mark (the constant high-bias guess). Pink dashed arrows show each student’s bias distance (Y − Ŷ) to that line and update automatically. Notice how it misses the green upward trend.
Note: High bias does not always look like a perfectly flat line, but predicting the same Ŷ for everyone is the classic underfitting example on this table.
Good fit — low bias
A low-bias model can follow the upward pattern instead of ignoring it. We use a second, cleaner table (still study hours → marks) where a straight line is a fair guess:
| Study hours (X) | Exam marks (Y) |
|---|---|
| 1 | 37 |
| 2 | 48 |
| 3 | 58 |
| 4 | 69 |
| 5 | 78 |
The canvas fits Ŷ = mX + b (green line) through these points. Errors Y − Ŷ are small and flip sign (+/−) around the line—that is low bias for this kind of trend. In the infographic this is the good fit (balanced) panel: the model is flexible enough to capture the pattern, not just the average.
Example 2 — canvas (good fit / low bias)
Same canvas UI as example 1, different data: green linear Ŷ, purple residual arrows (no flat average line).
Loading canvas…
Try it: nudge one mark and watch the green equation and purple residuals update. Compare average |Y − Ŷ| here with the pink arrows in example 1.
Note: Good fit on training data does not guarantee good performance on new students—that is where variance and generalization come in next.
Variance
Variance is how much predictions swing when the training data changes—even when the true pattern is similar. A high-variance model is too flexible: it chases every bump in the training table, including noise.
On our original study-hours table (35 → 65), imagine a model that is allowed to wiggle wildly instead of sticking to a simple line. It can memorize every training mark:
- With 5 rows, a polynomial of degree 4 can pass through all five dots.
- Training error (MSE on these rows) drops to about 0.
- Between the dots, the curve can shoot up or down—that instability is high variance. In ML we call this overfitting.
Why degree 4—and not 1, 2, 3, or 10?
Think in terms of n training points (here n = 5) and polynomial degree (how wiggly Ŷ is allowed to be):
| Degree | What you allow | On our 5-point table |
|---|---|---|
| 1 | A straight line only | Same idea as a simple linear fit—usually cannot hit every dot; training MSE stays positive. Often lower variance, can look like a good fit if the truth is roughly linear (example 2). |
| 2 | One bend (parabola) | More flexible than a line, but still may miss some marks; MSE often still positive. |
| 3 | Two bends | Closer to memorizing, but with 5 distinct hours there is still not enough freedom to force zero error on all rows in general. |
| 4 (= n − 1) | Three bends | Smallest degree that can interpolate all n points (one unique curve through every dot). Training MSE ≈ 0—classic memorization demo. |
| 10 (or any above n − 1) | Even more coefficients than we can pin down | With only 5 (x, y) pairs you do not get a single honest “degree 10” model—infinitely many high-degree curves pass through the same five dots. In practice we cap at n − 1 for a stable interpolating polynomial. Going to 10 does not add real information; it is redundant once degree 4 already hits every point. |
How students choose in real life: pick the simplest model that fits the pattern well on validation data—not the degree that drives training MSE to zero. Example 4 below lets you slide degree 1 → 4 and watch training MSE and the dashed curve change.
That is different from bias (example 1): there the model was too simple. Here the model is too complex for what we want to generalize.
| Idea | Example 1 (bias) | Example 3 (variance) |
|---|---|---|
| Model shape | Flat average | Wiggly curve through every point |
| Training fit | Poor (large errors) | Excellent (≈ zero error) |
| Risk on new students | Misses trend | Follows noise; unstable |
Example 3 — canvas (high variance / overfitting)
Same table as example 1; green dashed curve is the flexible Ŷ. Faded line is the simple linear trend for comparison.
Loading canvas…
Try it: change one mark—the dashed curve reshapes to still hit every visible point. Watch training MSE stay near zero while the between-point wiggles grow.
Note: Real overfitting in production is often more subtle than a degree-4 polynomial; the canvas exaggerates the idea so you can see memorization.
Example 4 — canvas (choose the degree)
Slider 1 … n−1 on the same table; badge shows training MSE and when you reach degree 4 you are in the high-variance memorizing regime.
Loading canvas…
Try it: start at degree 1, then 2, then 4. Ask: “Would I trust the wiggles between the dots for a new student?” That is how you separate good fit from high variance.
Underfitting and overfitting
You have now seen both failure modes on the same story (study hours → marks):
| Underfitting (high bias) | Overfitting (high variance) | |
|---|---|---|
| Canvas | Example 1 | Examples 3–4 (degree n−1) |
| Model | Too simple (flat Ŷ) | Too flexible (memorizes rows) |
| Training error | Usually high | Can be very low (even ≈ 0) |
| What went wrong | Misses the trend | Chases noise and wiggles between dots |
| Typical fix | Use a richer model (e.g. allow a line) | Use a simpler model or more data |
Balanced models sit in the middle—like example 2 when a line matches the trend without hugging every tiny bump.
Neither extreme is about “being wrong on purpose.” Both are about fit on the training table not matching what you need after training.
Generalization
Generalization means: how well the model works on new examples it did not train on.
In our story, “new” means a new student—study hours and marks that were not in the five training rows. That is the score that matters in real life, not how perfect Ŷ looks on rows you already memorized.
Training error vs new-student error
| Question | What it measures |
|---|---|
| Training error | Mistakes on rows used to fit the model (the five students in the table). |
| Generalization error | Mistakes on new students (or a held-out test set you hide while training). |
A model can look amazing on training and still fail new students—that is the overfitting trap from example 3.
How our examples generalize (plain English)
| Example | On the training five | On a new student (e.g. 6 hours) |
|---|---|---|
| 1 — flat Ŷ = 49 | Large errors; misses trend | Still predicts 49—ignores hours; often bad if marks rise with study time |
| 2 — line | Small errors; follows trend | Ŷ moves with hours—reasonable if the real world is also roughly linear |
| 3–4 — degree n−1 curve | Training MSE ≈ 0 | Curve wiggles between training dots; prediction at a new hour can swing—risky |
Key idea: Low training error does not prove the model will generalize. Example 3 is the clearest case: zero training MSE, but you would not trust the wild shape between the dots for someone new.
What good practice looks like (beginner version)
- Hold out some students (or some rows) and never use them while picking the model—only to check error at the end (a test set).
- Prefer the simplest model that does well on that held-out check (connects to example 4: degree 1 or 2 before degree n−1).
- Later you will use tools like cross-validation and regularization—they are formal ways to chase generalization, not just training MSE.
Try it — new student compare
Use the same training table as example 1. Pick study hours for a new student and (optionally) their true marks. See how Ŷ differs for the flat average, a line, and the degree-4 curve.
Loading generalization compare…
Try it: set X = 6 and Y = 70. Which Ŷ is closest? Change hours to 2.5 (between training dots) and notice how the memorizing curve can behave oddly between known points—that is variance hurting trust on new inputs.
One sentence to remember
Fit is for learning; generalization is for the students you have not seen yet.
What’s next in ML: measuring generalization properly (train/validation/test splits), tuning model complexity, and handling real datasets larger than five rows.
Summary
The three panels below match what you explored in the canvases: too simple (high bias), balanced (good fit), and too flexible (high variance).

| Diagram panel | Lesson canvas | Training error | New data |
|---|---|---|---|
| High bias — pink mean Ŷ = 49, same guess for every hour | Example 1 | Usually high | Usually poor |
| Good fit — purple trend line through the five marks | Example 2 | Low | Usually better |
| High variance — green curve that hits every training row | Examples 3–4 | Can be ≈ 0 | Often poor |
Generalization (above) is the real goal: low error on students and rows you did not train on—not just a perfect fit on the five in the table.