Skip to content
Home ML / AI Overfitting and Underfitting in ML — Causes, Detection and Fixes

Overfitting and Underfitting in ML — Causes, Detection and Fixes

Where developers are forged. · Structured learning · Free forever.
📍 Part of: ML Basics → Topic 4 of 25
Master the bias-variance tradeoff.
⚙️ Intermediate — basic ML / AI knowledge assumed
In this tutorial, you'll learn
Master the bias-variance tradeoff.
  • Underfitting = High Bias. The model is too dumb for the data.
  • Overfitting = High Variance. The model is too 'clever' and sees patterns in noise.
  • The validation set is your compass; never optimize based on the test set alone.
✦ Plain-English analogy ✦ Real code with output ✦ Interview questions
Quick Answer

Imagine you're studying for a history exam. If you memorise every question from last year's paper word-for-word, you'll ace a re-run but completely blank on any new question — that's overfitting. If you barely glance at the textbook and just guess 'World War 2' for everything, you'll fail because you learned too little — that's underfitting. A great student learns the patterns and principles, not the exact answers. Your ML model needs to do exactly the same thing.

Every ML model you build has one job: make good predictions on data it has never seen before. It sounds simple, but the single biggest reason models fail in production isn't bad algorithms or messy data — it's getting the balance of learning wrong. A model that learns too much from its training data becomes obsessed with noise and quirks that don't generalise. A model that learns too little never captures the real signal in the first place. Both failures have names, both are measurable, and both are fixable once you understand what's actually happening inside the model.

Overfitting and underfitting sit at opposite ends of a spectrum called the bias-variance tradeoff. Understanding this tradeoff is what separates engineers who tune models by intuition from those who tune them systematically. When you know WHY a model overfits, you stop throwing random regularisation at it and start making deliberate, principled decisions about complexity, data size, and training strategy.

By the end of this article you'll be able to plot a learning curve and diagnose whether your model is overfitting or underfitting just by looking at it. You'll have working Python code that deliberately creates both problems and then fixes them — so the concepts stick in your hands, not just your head. And you'll walk away knowing exactly which levers to pull in each scenario.

The Technical Root: Bias vs. Variance

To fix a failing model, you must diagnose its soul. Underfitting is caused by High Bias—the model makes simplistic assumptions about the data. Overfitting is caused by High Variance—the model is overly sensitive to small fluctuations in the training set. In a production environment, we use Learning Curves (plotting Error vs. Training Set Size) to visualize this struggle.

model_diagnostics.py · PYTHON
123456789101112131415161718192021222324252627
import numpy as np
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import learning_curve

# io.thecodeforge approach: Systematic Diagnostic
def generate_learning_curve(model, X, y):
    train_sizes, train_scores, test_scores = learning_curve(
        model, X, y, cv=5, scoring='neg_mean_squared_error'
    )
    
    # Calculate mean and standard deviation
    train_mean = -np.mean(train_scores, axis=1)
    test_mean = -np.mean(test_scores, axis=1)
    
    return train_sizes, train_mean, test_mean

# Underfitting: Linear model on non-linear data
underfit_model = LinearRegression()

# Overfitting: High-degree polynomial
overfit_model = Pipeline([
    ("poly_features", PolynomialFeatures(degree=15, include_bias=False)),
    ("std_scaler", StandardScaler()),
    ("lin_reg", LinearRegression()),
])
▶ Output
[Learning Curve Data Generated]
⚠ The Convergence Trap
In Underfitting, the training and validation curves converge quickly but at a high error rate. Adding more data won't help; you need a more complex model.

Production-Grade Implementation: Handling Model Persistence

When deploying models at TheCodeForge, we ensure that the model complexity is handled during the build phase. Below is a Java representation of a model metadata service that flags potential overfitting based on the gap between training and validation accuracy.

io/thecodeforge/ml/ModelGuard.java · JAVA
12345678910111213141516171819202122232425
package io.thecodeforge.ml;

import java.util.logging.Logger;

public class ModelGuard {
    private static final Logger logger = Logger.getLogger(ModelGuard.class.getName());
    private static final double OVERFIT_THRESHOLD = 0.15; // 15% gap

    public static void validateModelPerformance(double trainAcc, double valAcc) {
        double gap = Math.abs(trainAcc - valAcc);
        
        if (trainAcc < 0.60 && valAcc < 0.60) {
            logger.warning("CRITICAL: Model is UNDERFITTING. High Bias detected.");
        } else if (gap > OVERFIT_THRESHOLD) {
            logger.warning("CRITICAL: Model is OVERFITTING. High Variance detected. Gap: " + gap);
        } else {
            logger.info("Model state is HEALTHY. Generalization optimal.");
        }
    }

    public static void main(String[] args) {
        // Example: High Training Accuracy, Low Validation
        validateModelPerformance(0.98, 0.72);
    }
}
▶ Output
WARNING: CRITICAL: Model is OVERFITTING. High Variance detected. Gap: 0.26
FeatureUnderfitting (High Bias)Overfitting (High Variance)
Training ErrorHighVery Low
Validation ErrorHighHigh
CauseModel is too simple (e.g., Linear for Non-linear)Model is too complex (e.g., Deep Tree for noisy data)
Primary FixIncrease complexity, add featuresRegularization (L1/L2), Pruning, More Data

🎯 Key Takeaways

  • Underfitting = High Bias. The model is too dumb for the data.
  • Overfitting = High Variance. The model is too 'clever' and sees patterns in noise.
  • The validation set is your compass; never optimize based on the test set alone.
  • Regularization (Dropout, L1, L2) is the primary weapon against overfitting.
  • The forge only works when the heat (complexity) is balanced 🔥

⚠ Common Mistakes to Avoid

    Thinking 'More Data' fixes everything (it won't fix high bias/underfitting).
    Fix

    high bias/underfitting).

    Ignoring the Validation Set performance until the end of the project.
    Over-tuning hyperparameters on the Test set (Data Leakage).
    Using a high-degree polynomial regression on a small dataset without L2 regularization.

Interview Questions on This Topic

  • QHow do you distinguish between high bias and high variance using a learning curve?Reveal
    High Bias is identified when both training and validation errors are high and close to each other, indicating the model hasn't captured the underlying trend. High Variance is identified by a large 'gap' between a low training error and a significantly higher validation error.
  • QWhat is the 'Double Descent' phenomenon in modern Deep Learning?Reveal
    It is a counter-intuitive observation where increasing model complexity past the interpolation threshold (where training error is zero) actually causes validation error to decrease again, challenging the classical Bias-Variance Tradeoff.
  • QExplain the role of Early Stopping in preventing overfitting.Reveal
    Early Stopping monitors the validation loss during training and halts the process as soon as the validation loss starts to rise, even if the training loss is still falling. This prevents the model from 'memorizing' noise.

Frequently Asked Questions

Can a model be both overfit and underfit?

Technically, yes, in complex multi-output models or deep neural networks where different layers or regions of the data exhibit different behaviors (e.g., overfitting on one class but underfitting on another due to class imbalance).

Why does regularization reduce overfitting?

Regularization adds a penalty term to the loss function based on the size of the model weights. This discourages the model from relying too heavily on any single feature, forcing it to find simpler, more robust patterns.

Does increasing the number of features cause overfitting?

Yes, this is known as the 'Curse of Dimensionality.' As you add more features, the model has more opportunities to find accidental correlations that don't exist in the broader population.

🔥
Naren Founder & Author

Developer and founder of TheCodeForge. I built this site because I was tired of tutorials that explain what to type without explaining why it works. Every article here is written to make concepts actually click.

← PreviousML Workflow — Data to DeploymentNext →Train Test Split and Cross Validation
Forged with 🔥 at TheCodeForge.io — Where Developers Are Forged