When the Model Memorizes (Overfitting)

How to notice that the model didn't learn but memorized.

Fundamentals 7 min Beginner May 18, 2026

Your ML model scores 99% on training data — but crashes to 55% on new data. The model didn't learn to solve the problem. It memorized the answers. This is called overfitting, and it's the single most common failure mode in machine learning.

In this article, you'll learn why overfitting happens, how to detect it, and which tools help you avoid it.

The Memorizing Student — What Is Overfitting?

Overfitting

AnalogyDefinition
Imagine a student preparing for a math exam who memorizes all practice problems with their exact solutions — without understanding the method. When slightly different problems appear on the actual exam, the student fails because they never learned how to solve, only how to recall.

Unlike a student, an ML model has no 'understanding' — it optimizes a loss function. The analogy works for intuition but shouldn't suggest that models have cognitive processes.

Example: Polynomial Fitting

Imagine 10 data points with slight noise following a quadratic trend.

  • Degree 1 (straight line): Misses the curve entirely. This is underfitting — the model is too simple.
  • Degree 3: Captures the trend well. The curve follows the pattern without memorizing every fluctuation.
  • Degree 15: Passes through every single point but oscillates wildly between them. The model memorized the noise. Training error is zero — but for any new point, it gives absurd predictions.

Myth: High training accuracy = good model

This is the most dangerous misconception in machine learning. A model with 99.9% training accuracy may be severely overfitted. The ONLY meaningful metric is validation or test performance. Training accuracy only tells you the model can memorize — not that it can generalize.

Every very complex model (like today's large AI systems) tends to memorize data. This is why developers monitor both training loss and validation loss. When validation loss starts rising while training loss keeps falling, you're overfitting — and that's when you stop training (Early Stopping).

Underfitting

AnalogyDefinition
Besides memorization, there is a second extreme — a student who only reads the chapter headings and then takes the exam. Too little effort to even recognize the basic patterns.
Overfitting

Symptom: Low training error, high test error. Cause: Model too complex or trained too long. Solution: Simplify model, more data, regularization, early stopping.

Underfitting

Symptom: High training error AND high test error. Cause: Model too simple or trained too little. Solution: More complex model, more features, train longer.

Interactive: Underfitting vs. Overfitting

Move the slider to see how an underfitted model (left) differs from an overfitted model (right). The sweet spot is in the middle — the model captures real patterns without memorizing noise.

Underfitting vs. Overfitting

Move the slider to switch between underfitting (left) and overfitting (right). The blue dots are training data. The curve shows how the model interprets the data.

UnderfittingOverfitting
Auto
‹ ›
📉

Underfitting

The model is too simple. It doesn't even recognize the obvious patterns in the training data. Like a student who hasn't understood the task.

Model complexityToo low
Training errorHigh
Test errorHigh
📈

Overfitting

The model is too complex. It memorizes every single data point, including noise. Like a student who memorizes answers instead of understanding.

Model complexityToo high
Training errorVery low
Test errorHigh
🎯
Sweet Spot: Good Fit

The optimal compromise lies in the middle: complex enough to recognize real patterns, but simple enough to generalize to new data. Techniques like regularization, cross-validation, and early stopping help find this point.

The U-Shaped Curve — Finding the Sweet Spot

Imagine a chart: model complexity (e.g., polynomial degree) on the X-axis, error on the Y-axis. What emerges?

Training error decreases steadily with increasing complexity — the model fits the training data ever more closely. But test error behaves differently: it drops first (as the model improves), then rises again (as the model starts learning noise).

This U-shaped test curve is the central diagnostic tool. At the lowest point of the U lies the optimal point — the best balance between too simple (underfitting, left) and too complex (overfitting, right).

The bias-variance tradeoff explains WHY the U-curve exists.

Bias = the systematic error from oversimplifying the model. High bias means the model misses the real patterns (underfitting).

Variance = the model's sensitivity to fluctuations in training data. High variance means the model reacts to noise instead of patterns (overfitting).

Total error = Bias² + Variance + Irreducible noise

Think of archery: High bias = you consistently hit the wrong spot (arrows clustered but off-center). High variance = arrows scattered everywhere (even if centered on average). You want both low: a tight cluster right on the bullseye.

Unlike real archery, you can't simply 'aim better' in ML. Using a more powerful bow (complex model) can scatter the arrows more (higher variance).

Reducing bias (more complex model) almost always increases variance. Reducing variance (simpler model) increases bias. The optimal point minimizes the sum.

Train, Validate, Test — The Three-Exam System

How do you know if your model generalizes? You test it on data it has never seen. For this, the dataset is split into three disjoint parts:

1
Training (60-70%): Daily homework. The model learns from this data.
2
Validation (15-20%): Mock exam. You tune hyperparameters and monitor overfitting.
3
Test (15-20%): Final exam. Used exactly ONCE at the end.

Like school: homework is for practice, the mock exam shows where you stand, and the final exam can only be taken once. Anyone who peeks at the real exam and studies from it no longer has a valid result.

The golden rule: Test data used only ONCE

If you use the test set to make decisions (e.g., model selection or hyperparameter tuning), it loses its validity. It effectively becomes a second validation set, and you have no unbiased estimate of real-world performance left.

Practice: Splitting data with scikit-learn

from sklearn.model_selection import train_test_split

# Step 1: 70% training, 30% temporary
X_train, X_temp, y_train, y_temp = train_test_split(
    X, y, test_size=0.3, random_state=42
)

# Step 2: Split temporary into 15% validation + 15% test
X_val, X_test, y_val, y_test = train_test_split(
    X_temp, y_temp, test_size=0.5, random_state=42
)

For small datasets, a single split is not enough. Cross-validation helps: the dataset is divided into k parts ('folds'), and training is repeated k times, with each fold serving as validation once.

What Can You Do?

Overfitting is not inevitable. These four strategies help:

More Data More training data makes it harder for the model to memorize noise. When possible, this is the most effective strategy.
Simpler Model Fewer parameters = fewer degrees of freedom for memorization. E.g., polynomial degree 3 instead of 15.
Regularization Penalizes extreme parameter values (L1/L2). Forces the model to prefer simpler solutions.
Early Stopping Stop training once validation error stops decreasing. Prevents the model from adapting to noise.

Interactive: Why Complexity Explodes

Move the slider to see how fast the computational costs of different model complexities grow. An O(n²) model already needs 10,000 operations at 100 data points — a key reason why regularization matters.

1500
O(1)1
O(n)100
O(n²)10.000
O(2ⁿ)1.073.741.824
Moderate Input

At n=100, the difference becomes visible: O(n²) requires 10.000 operations, while O(n) needs only 100. O(log n) needs just 6.6 — that's 15x less than O(n).

Ratio to O(n)

ComplexityOperationsFactor vs. O(n)
O(1)1100x faster
O(n)1001x (Reference)
O(n²)10.000100x slower
O(2ⁿ)1.073.741.82410737418x slower

Summary

  1. Overfitting = memorizing noise instead of learning patterns. Symptom: low training error + high test error.
  2. The sweet spot lies between underfitting (too simple) and overfitting (too complex) — visible in the U-shaped test curve.
  3. Always split your data: Train to learn, Validate to tune, Test to evaluate — and use the test set exactly once.

Quiz: Overfitting

Question 1 / 4
Not completed

What is overfitting?

Select one answer
Answer Key: 1) B · 2) C · 3) C · 4) B

Understanding Check

  • Why is high training accuracy alone a dangerous metric?
  • What is the difference between validation set and test set — and why can the test set only be used once?
  • What happens to bias and variance when you increase model complexity?