
Imagine three students sitting for the same exam.
Kiran barely studied. He writes almost the same answer to every question.
Ravi found last year's question paper. He memorized it, answer by answer, word for word.
Anil studied the ideas behind the questions. He understands why the answers are what they are.
First, the teacher gives them last year's paper again. Ravi scores 100. Anil scores 85. Kiran scores 30. Ravi looks like the star of the class.
Then the teacher gives them this year's paper. Same subject, but new questions. Ravi scores 35. Anil scores 82. Kiran scores 30.
Same students. Same subject. And the one who looked the smartest just fell apart.
A machine learning model can be any one of these three students. And the scary part is this: if you only check the model on the data it already studied, Ravi looks like a genius. This post is about how to catch him.
Our Exam: How Long Do People Watch a Reel?
Let's give our model a real task. We want to predict the average watch time of a reel, based on how long the reel is.
Here are ten reels the model gets to study. These are its practice paper.
| Reel length (seconds) | Average watch time (seconds) |
|---|---|
| 5 | 12.5 |
| 10 | 16.6 |
| 15 | 18.5 |
| 20 | 19.5 |
| 25 | 21.2 |
| 30 | 20.4 |
| 35 | 21.2 |
| 40 | 21.4 |
| 50 | 12.5 |
| 60 | 2.6 |
Look at the pattern. Very short reels get watched almost fully, but they are short, so the watch time is small. Medium reels, around 25 to 40 seconds, hold people the longest. Very long reels lose people fast, so the watch time drops. It rises, peaks, and falls.
Now here are six more reels. The model never sees these while studying. This is the new exam paper.
| Reel length (seconds) | Average watch time (seconds) |
|---|---|
| 8 | 15.5 |
| 18 | 20.7 |
| 28 | 22.2 |
| 38 | 18.8 |
| 45 | 16.8 |
| 55 | 9.9 |
Three Models, Three Students
We will build three models on the practice reels, using the polynomial idea from the post on bending the line. The only difference between them is how much freedom they have to bend.
Model 1 is a straight line. Just w times x plus b. It is Kiran, the lazy student.
Model 2 is a gentle curve, a line that is allowed to bend once. It is Anil.
Model 3 is a very wiggly curve, one that can bend up to nine times. It is Ravi.
The blue dots are the reels the model studied. The red circles are the new reels it has never seen.
Look at the left panel first. The straight line cuts through the middle and misses almost everything. It cannot bend, so it cannot follow the rise and the fall. It is wrong on the practice reels and wrong on the new reels.
Now the middle panel. The curve follows the real pattern. It does not touch every dot, and that is fine. The red circles land close to the curve too.
Now the right panel. The wiggly curve goes through every single blue dot. Perfect on the practice paper. But look at what it does between and beyond the dots. It dips, it jumps, and at the far right it shoots off the top of the chart. The red circles are nowhere near it.
Putting Numbers on It
Pictures help, but let's check with numbers. We use the same cost function from the third post of this series. It measures the average squared miss, divided by 2.
First, let's do one new reel by hand. Take the 55 second reel. Its real watch time is 9.9 seconds. Here is what each model predicts.
| Model | Prediction | Miss (prediction minus actual) | Miss squared |
|---|---|---|---|
| Straight line | 12.8 | 2.9 | 8.41 |
| Gentle curve | 8.4 | -1.5 | 2.25 |
| Wiggly curve | 80.5 | 70.6 | 4984.36 |
Read the last row again. The wiggly model said people would watch this reel for 80.5 seconds. The reel is only 55 seconds long. It is not just wrong, it is impossible. Squaring that miss gives almost 5,000. One bad prediction like this is enough to wreck the whole cost.
Now the cost over all the reels, for the practice reels and for the new reels.
| Model | Cost on practice reels | Cost on new reels |
|---|---|---|
| Straight line | 13.08 | 6.67 |
| Gentle curve | 0.47 | 0.44 |
| Wiggly curve | 0.00 | 419.91 |
This table is the whole lesson. Let's read it like a teacher reading report cards.
The straight line is bad on both. That is Kiran. It did not learn enough.
The gentle curve is low on both, and the two numbers are close to each other. That is Anil. It learned the real pattern.
The wiggly curve scores exactly zero on the practice reels. Perfect. Then 419.91 on the new reels. That is Ravi. It did not learn the pattern. It memorized the dots, including all the little random noise in them.
The Names for These Two Problems
Machine learning people have names for these two failures.
Underfitting is when the model is too simple for the pattern. Another name for it is high bias. Think of bias as a stubborn built-in belief. A straight line is stubbornly sure that the world is straight. It will be wrong in the same way, no matter which reels you show it.
Overfitting is when the model is too flexible and learns the noise along with the pattern. Another name for it is high variance. Variance means how much the model changes when the data changes a little. Let's test that, because it is the most surprising part of this post.
What Happens If One Reel Changes a Little?
Let's take one practice reel, the 35 second one, and change its watch time from 21.2 to 24.2. That is a tiny change, just 3 seconds on one reel out of ten. Then we train all three models again and see how much their predictions move.
| Model | Prediction at 45 s, before | Prediction at 45 s, after | Prediction at 55 s, before | Prediction at 55 s, after |
|---|---|---|---|---|
| Straight line | 14.3 | 14.7 | 12.8 | 13.3 |
| Gentle curve | 16.4 | 16.9 | 8.4 | 8.6 |
| Wiggly curve | 11.3 | -5.5 | 80.5 | 206.5 |
The straight line and the gentle curve barely move. Half a second here, a fifth of a second there.
The wiggly curve goes wild. At 45 seconds, it changes from 11.3 to negative 5.5. A negative watch time. At 55 seconds, it jumps from 80.5 to 206.5. A change of 3 seconds in one data point shook the whole model.
That is what high variance means. The model is so eager to match every dot that it follows the noise, and noise changes every time. You cannot trust something that changes its mind this much.
How to Catch Ravi
So how do we catch an overfit model before it embarrasses us? With the same trick the teacher used. Test it on questions it has not seen. Then compare two numbers: the error on the practice data and the error on the new data.
| What you see | What it means |
|---|---|
| Both errors are high | Underfitting. The model is too simple. |
| Practice error is tiny, new error is large | Overfitting. The model memorized. |
| Both errors are low and close together | A good fit. |
You can check this in the cost table above. We will set up this check properly later in this series, with a clean way to split your data. For now, remember the habit: never judge a model only by the data it studied.
Turning This Into Code
Let's start with the data, and the cost function we already know, loop first and then vectorized.
import numpy as np
train_length = np.array([5, 10, 15, 20, 25, 30, 35, 40, 50, 60], dtype=float)
train_watch = np.array([12.5, 16.6, 18.5, 19.5, 21.2, 20.4, 21.2, 21.4, 12.5, 2.6])
new_length = np.array([8, 18, 28, 38, 45, 55], dtype=float)
new_watch = np.array([15.5, 20.7, 22.2, 18.8, 16.8, 9.9])
def cost(predictions, actual):
m = len(actual)
total = 0
for i in range(m):
total += (predictions[i] - actual[i]) ** 2
return total / (2 * m)
def cost_vectorized(predictions, actual):
return np.sum((predictions - actual) ** 2) / (2 * len(actual))
Remember the lesson from the feature scaling post. Big numbers raised to big powers get out of control quickly, so we scale the lengths first. And we use the mean and spread of the practice data for the new reels too, not freshly calculated ones.
mean = train_length.mean()
spread = train_length.std()
def scale(x):
return (x - mean) / spread
Now we train a model for each degree. A quick note. In the earlier posts, we used gradient descent to find the weights. For high powers, gradient descent becomes slow and fiddly, so here we use a ready-made tool called np.polyfit. It finds the weights with the lowest cost directly. It is the same goal, reached by a shortcut.
for degree in [1, 2, 3, 5, 9]:
weights = np.polyfit(scale(train_length), train_watch, degree)
train_predictions = np.polyval(weights, scale(train_length))
new_predictions = np.polyval(weights, scale(new_length))
print(f"degree {degree}: practice cost = {cost_vectorized(train_predictions, train_watch):.2f}, "
f"new reels cost = {cost_vectorized(new_predictions, new_watch):.2f}")
This prints:
degree 1: practice cost = 13.08, new reels cost = 6.67
degree 2: practice cost = 0.47, new reels cost = 0.44
degree 3: practice cost = 0.33, new reels cost = 0.59
degree 5: practice cost = 0.13, new reels cost = 1.57
degree 9: practice cost = 0.00, new reels cost = 419.91
Watch the two columns as the degree goes up. The practice cost only goes down. Of course it does, more bending means more room to match the dots. But the new reels cost goes down at first, hits its lowest point at degree 2, and then climbs. By degree 9 it explodes. Getting better on the practice paper does not always mean getting better at the real exam.
And here is the shake test from earlier, nudging one reel and watching the predictions move.
nudged_watch = train_watch.copy()
nudged_watch[6] += 3.0 # the 35 second reel: 21.2 becomes 24.2
for degree in [1, 2, 9]:
before = np.polyfit(scale(train_length), train_watch, degree)
after = np.polyfit(scale(train_length), nudged_watch, degree)
p_before = np.polyval(before, scale(55))
p_after = np.polyval(after, scale(55))
print(f"degree {degree}: prediction at 55 s moved from {p_before:.1f} to {p_after:.1f}")
degree 1: prediction at 55 s moved from 12.8 to 13.3
degree 2: prediction at 55 s moved from 8.4 to 8.6
degree 9: prediction at 55 s moved from 80.5 to 206.5
Where We Go From Here
So what do we do about Ravi? There are a few honest options. Collect more data, so there is less room to memorize. Use a simpler model, with fewer bends. Or here is a third idea, and it is a clever one.
What if we keep the powerful, flexible model, but teach it to stay humble? Not to throw away its extra bends, just to use them only when they are really worth it. It turns out one small addition to our cost function does exactly this, and it is simpler than it sounds. That is what the next post is about.
The full notebook for this post is up on GitHub here: github.com/tharun-pandya/Machine-Learning-Blog-Series/blob/main/Overfitting_vs_Underfitting.ipynb
See you in the next one.
Comments
Join the discussionUsing the reel-length versus watch-time curve—where watch time rises, peaks around 25 to 40 seconds, and drops sharply by 60 seconds—is a much better teaching dataset than synthetic sine waves because students immediately see why a straight line underfits the inverted-U shape while a degree-9 polynomial chases every noise point on ten training reels and collapses on the six held-out ones.
Pairing that visual gap with a quick note on why real production splits also need time-ordered or group-aware holdouts (so duplicate creators or correlated sessions do not leak across train and test) bridges the intuition neatly into applied ML workflows.


