Anybody Can AI

Quick Stats

Completed

0

Time Spent

0m

Streak

0

User

User

Machine Learning Fundamentals

Supervised Learning Deep Dive/Overfitting and Underfitting

Overfitting and Underfitting

Understand the most common problems in machine learning and how to identify and fix them.

The Two Biggest Challenges in Machine Learning

The two biggest challenges in machine learning aren't about getting a model to work - they're about getting it to work on new, unseen data.

Overfitting and underfitting are the reasons most ML projects fail to perform in production.

🧠 The Goldilocks Problem

Imagine teaching a student for an exam:

  • Underfitting: The student barely studies, doesn't understand the concepts, fails the exam.
  • Just Right: The student understands the concepts, applies them to new problems, passes the exam.
  • Overfitting: The student memorizes every practice problem word-for-word but can't solve new variations, fails the exam.

Machine learning models face the same challenge.

🔹 What is Underfitting?

Underfitting happens when your model is too simple to capture the patterns in your data.

Example: Predicting House Prices

Your data shows:

  • House size matters
  • Number of bedrooms matters
  • Location matters
  • Age of house matters

Your simple model only considers:

  • House size

Result: Predictions are consistently inaccurate because you're missing important information.

Signs of underfitting:

  • Low accuracy on training data (can't even learn the examples it sees)
  • Low accuracy on test data
  • Model predictions seem too simplistic

Visual analogy: Trying to fit a straight line through data that curves.

🔹 What is Overfitting?

Overfitting happens when your model learns the training data too well—including noise and random fluctuations that don't represent real patterns.

Example: Predicting House Prices

Training data shows:

  • House #42: 1,500 sq ft, sold for $250,000
  • House #43: 1,502 sq ft, sold for $320,000

An overfit model thinks:

"Ah! At exactly 1,502 sq ft, houses jump $70k in value!"

In reality, house #43 might have had a bidding war, renovated kitchen, or better view—factors not in your data. The model memorized noise.

Signs of overfitting:

  • Very high accuracy on training data (99%+)
  • Much lower accuracy on test data (70%)
  • Model predictions are overly specific and fragile
  • Adding more data doesn't help much

Visual analogy: Drawing a wiggly line that touches every single data point, including outliers.

📉 The Training vs. Test Performance Gap

This graph tells the story:

As model complexity increases:

  • Training error keeps decreasing (model gets better at memorizing)
  • Test error decreases at first, then increases (model stops generalizing)

The sweet spot:

  • Training error: Reasonably low
  • Test error: Close to training error
  • Gap between them: Minimal

Scenario

Training Error

Test Error

Result

Healthy Model

5%

6%

✅ Good Generalization

Severe Overfitting

1%

25%

❌ Overfitting

Underfitting

40%

42%

❌ Too Simple

⚠️ Detecting Overfitting

1. Performance Divergence

Epoch

Training Accuracy

Test Accuracy

1

80%

78%

10

90%

88%

50

98%

85%

100

99%

82%

Notice test performance getting worse while training improves?
Classic overfitting.

2. Model Complexity vs. Data Size

  • 10 features, 100 examples → Probably fine
  • 100 features, 100 examples → Risk of overfitting
  • 1000 features, 100 examples → Definitely overfitting

Rule of thumb: You need at least 10 examples per feature.

3. Validation Curve

Plot model performance against a hyperparameter (like tree depth):

  • Depth 1–5: Both train and test improve
  • Depth 6–10: Train keeps improving, test plateaus
  • Depth 10+: Train near perfect, test declining

Use the depth where test performance peaks (around 6–7).

🛠️ How to Fix Underfitting

  1. Use a More Complex Model
    • Linear → Polynomial
    • Simple decision tree → Random forest
    • Shallow neural network → Deeper neural network
  2. Add More Features
    • Add: bedrooms, bathrooms, age, location, school district
  3. Reduce Regularization
    • If you're constraining the model too much, loosen the constraints.
  4. Train Longer
    • Sometimes the model just needs more iterations to learn.
  5. Check Your Data
    • Maybe the features genuinely don't predict the output well. You might need different data entirely.

🧩 How to Fix Overfitting

  1. Get More Training Data
    • The single best solution. More examples make it harder to memorize.
    • 1,000 examples → 10,000 examples
    • Overfitting often disappears naturally
  2. Simplify the Model
    • 10-layer neural network → 3-layer
    • Depth-20 tree → Depth-5 tree
    • 100 features → 20 most important features
  3. Use Regularization
    • L1 Regularization: Pushes unnecessary weights to zero
    • L2 Regularization: Keeps all weights small
    • Dropout (neural networks): Randomly disable neurons during training
  4. Early Stopping
    • Stop training when test performance stops improving.
      Example:
    • Epoch 25: Test accuracy peaks at 87%
    • Epochs 26–50: Test accuracy declines to 83%
    • ✅ Use the model from epoch 25
  5. Cross-Validation
    • Instead of one train/test split, use multiple:
      • Split 1: Train on A+B, test on C
      • Split 2: Train on A+C, test on B
      • Split 3: Train on B+C, test on A
      • Average the results
  6. Data Augmentation
    • Create variations of training data:
      • Images: Rotate, flip, crop, adjust brightness
      • Text: Synonym replacement, paraphrasing
      • Audio: Add background noise, change speed

This artificially increases your dataset size.

⚖️ The Bias–Variance Tradeoff

Technical terms for this concept:

  • Underfitting = High Bias
    • Model has strong assumptions
    • Ignores patterns in data
    • Too rigid
  • Overfitting = High Variance
    • Model has weak assumptions
    • Follows every fluctuation
    • Too flexible

Goal: Find the balance between bias and variance.

Model Type

Bias

Variance

Simple Models

High

Low

Complex Models

Low

High

Just Right

Moderate

Moderate

💡 Practical Tips

During Development:

  1. Always use separate train/validation/test sets
  2. Monitor both training and validation metrics during training
  3. If training accuracy >> validation accuracy → You're overfitting
  4. If both are low → You're underfitting
  5. Start simple, add complexity gradually
  6. More data almost always helps overfitting

In Production:

  1. Monitor model performance over time
  2. Models can start overfitting to recent data (concept drift)
  3. Retrain periodically with fresh data
  4. Set up alerts for performance degradation

💼 Real-World Example: Fraud Detection System

Initial model:

  • Training accuracy: 99%
  • Test accuracy: 72%
  • Problem: Severe overfitting

Fixes applied:

  1. Reduced features from 200 to 50 (removed correlated ones)
  2. Added regularization (L2 penalty)
  3. Used 5-fold cross-validation
  4. Implemented early stopping

Final model:

  • Training accuracy: 91%
  • Test accuracy: 89%
    ✅ Problem solved! Model generalizes well.

Lower training accuracy, but much better at handling new fraud patterns.

🏁 Key Takeaway

Overfitting and underfitting are inevitable challenges.
The key is:

  1. Recognize them early (monitor train vs. test performance)
  2. Apply the right fixes (more data, regularization, model complexity adjustments)
  3. Validate thoroughly before deployment

🔜 In the Next Lesson

We'll explore how to select the right evaluation metrics for your specific problem.