Skip to content
C

Overfitting & Underfitting

Complete learning notes


1. Introduction

In Module 6, you learned about the Bias-Variance Tradeoff conceptually. This topic translates that theory into the two practical problems you'll actually encounter and need to fix in real ML projects: overfitting and underfitting. Recognizing these two failure modes — and knowing how to fix each — is one of the most essential practical skills in Machine Learning.


2. What are Overfitting and Underfitting?

Simple definition: Overfitting happens when a model learns the training data TOO well — including its noise and quirks — and performs poorly on new data. Underfitting happens when a model is too simple to capture the real patterns in the data, performing poorly even on the training data itself.

Technical explanation: Overfitting corresponds to a high-variance model that has essentially memorized the training set's specific characteristics (including noise), resulting in a large gap between training and test performance. Underfitting corresponds to a high-bias model whose capacity or feature representation is insufficient to capture the true underlying relationship, resulting in poor performance across both training and test sets.


3. Why is it Important?

  • These are the two most common reasons a real-world ML model fails to perform well.
  • Correctly diagnosing WHICH problem you have (overfitting vs underfitting) determines which fix will actually help.
  • Nearly every technique in the rest of this module (Regularization, Hyperparameter Tuning, Feature Engineering, Ensembles) exists specifically to address one or both of these problems.

4. Prerequisites

Comfort with the Bias-Variance Tradeoff (Module 6, Topic 6) and Train-Test Split (Module 3, Topic 9).


5. Core Concepts

  1. Overfitting (high variance)
  2. Underfitting (high bias)
  3. Diagnosing via train vs test performance gap
  4. General strategies to fix each problem

6. Detailed Explanation

a) Overfitting

An overfit model performs excellently on training data but noticeably worse on test/new data. It has essentially "memorized" the training examples, including irrelevant noise, rather than learning the true, generalizable underlying pattern.

b) Underfitting

An underfit model performs poorly on BOTH training and test data. It's too simple (or lacks the right features) to capture the actual relationship in the data at all.

c) Diagnosing via Train vs Test Gap

  • Poor training AND poor test performance → Underfitting.
  • Excellent training performance BUT noticeably worse test performance → Overfitting.
  • Good performance on both → the model is well-balanced (the goal).

d) General Fix Strategies

  • For overfitting: simplify the model, gather more training data, apply regularization (Topic 2), use ensemble techniques (Topic 5), or use Cross-Validation (Module 6, Topic 4) during tuning.
  • For underfitting: increase model complexity, add more relevant features (Feature Engineering, Topic 4), or reduce excessive regularization if already applied.

7. How It Works

  1. Train your model and evaluate it on BOTH the training set and a separate test set.
  2. Compare the two performance scores.
  3. If both are poor → underfitting → increase model complexity or improve features.
  4. If training is much better than test → overfitting → simplify the model, regularize, or gather more data.
  5. Re-evaluate after applying a fix, and repeat as needed.

8. Real-World Example

Imagine studying for an exam. Underfitting is like barely skimming the textbook once — you'll perform poorly on both easy practice questions AND the real exam, since you haven't genuinely learned the material. Overfitting is like memorizing the exact wording and answers of last year's practice exam word-for-word — you'll ace THAT specific practice test, but perform poorly on the real exam, since the actual questions are phrased differently and memorization doesn't transfer to genuine understanding.


9. Python Example

python
from sklearn.tree import DecisionTreeRegressor from sklearn.metrics import mean_squared_error from sklearn.model_selection import train_test_split import numpy as np np.random.seed(42) X = np.linspace(0, 10, 40).reshape(-1, 1) y = np.sin(X).ravel() + np.random.normal(0, 0.15, 40) X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42) configs = { "Underfit (max_depth=1)": DecisionTreeRegressor(max_depth=1, random_state=42), "Well-balanced (max_depth=4)": DecisionTreeRegressor(max_depth=4, random_state=42), "Overfit (max_depth=None)": DecisionTreeRegressor(max_depth=None, random_state=42), } for name, model in configs.items(): model.fit(X_train, y_train) train_mse = mean_squared_error(y_train, model.predict(X_train)) test_mse = mean_squared_error(y_test, model.predict(X_test)) print(f"{name} — Train MSE: {train_mse:.4f}, Test MSE: {test_mse:.4f}")

Expected Output (approximate):

text
Underfit (max_depth=1) — Train MSE: 0.3654, Test MSE: 0.4012 Well-balanced (max_depth=4) — Train MSE: 0.0298, Test MSE: 0.0512 Overfit (max_depth=None) — Train MSE: 0.0008, Test MSE: 0.1523

10. Code Explanation

  • The underfit model (max_depth=1) shows high error on BOTH training and test data — it's too simple to capture the sine-wave pattern.
  • The well-balanced model (max_depth=4) shows reasonably low error on BOTH sets, with only a modest gap between them — this is the target we're aiming for.
  • The overfit model (max_depth=None, unlimited depth) shows an almost perfect training score, but noticeably worse test performance — a clear sign it has memorized training-specific noise rather than the genuine underlying pattern.
  • This direct three-way comparison illustrates exactly how to diagnose which regime a model is in, simply by comparing its train and test scores side by side.

11. Advantages (of Understanding This Concept)

  • Provides a clear, actionable diagnostic framework for troubleshooting any underperforming model.
  • Directly connects theory (Bias-Variance, Module 6) to practical action (what to actually change).
  • Applies universally across virtually every ML algorithm covered in this course.

12. Limitations

  • Diagnosis based on train/test comparison is a useful heuristic, but doesn't always pinpoint the EXACT fix needed — some experimentation is usually still required.
  • A single train-test split can sometimes give a misleading picture; Cross-Validation (Module 6, Topic 4) provides a more robust diagnosis.

13. Common Mistakes

  • Only checking training performance, and missing an overfitting problem entirely (since training performance alone always looks good when overfit).
  • Automatically assuming a low-performing model is "underfitting" without actually checking test performance.
  • Applying an overfitting-focused fix (like simplifying the model) to a model that's actually underfitting, making the problem worse.

14. Best Practices

  • ALWAYS evaluate on both training and test/validation data — never rely on training performance alone.
  • Use Cross-Validation (Module 6, Topic 4) for a more reliable diagnosis before deciding on a fix.
  • Address the SPECIFIC problem you've diagnosed — don't apply overfitting fixes to underfitting problems, or vice versa.

15. Real-World Applications

  • Diagnosing why a model that performed great in development suddenly performs poorly in production (often overfitting).
  • Guiding hyperparameter choices (Topic 3) based on observed train/test performance gaps.
  • A standard diagnostic step in virtually every real-world ML project's development cycle.

16. Interview-Oriented Points

  • Be ready to clearly define overfitting and underfitting, and how to diagnose each from train/test performance.
  • Understand which general strategies address each problem.
  • Be able to connect this topic directly back to the Bias-Variance Tradeoff (Module 6).

17. Exam-Oriented Points

  • Overfitting: great training performance, poor test performance (high variance).
  • Underfitting: poor performance on BOTH training and test (high bias).
  • Fixes for overfitting: simplify model, more data, regularization, ensembles. Fixes for underfitting: increase complexity, better features.

18. Comparison Table — Overfitting vs Underfitting

AspectOverfittingUnderfitting
Training performanceExcellentPoor
Test performancePoor (large gap from training)Poor (similar to training)
Underlying issueHigh variance — model too complex/sensitiveHigh bias — model too simple
Typical fixSimplify model, regularize, more dataIncrease complexity, better features

19. Quick Revision

  • Overfitting = excellent training performance but poor test performance (high variance); model has memorized training-specific noise.
  • Underfitting = poor performance on both training and test (high bias); model is too simple to capture real patterns.
  • Diagnose by comparing train vs test performance; fix accordingly (simplify vs increase complexity).
  • This topic directly builds on the Bias-Variance Tradeoff (Module 6, Topic 6).

Mock Test

  • Overfitting & Underfitting — Quick Test

    A 10-question multiple-choice check on Overfitting & Underfitting.

    10 questions · 10 min · Easy
    Start Mock Test

Coding Problems