Overfitting and Underfitting in ML — Causes, Detection and Fixes
- 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.
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.
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()), ])
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.
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); } }
| Feature | Underfitting (High Bias) | Overfitting (High Variance) |
|---|---|---|
| Training Error | High | Very Low |
| Validation Error | High | High |
| Cause | Model is too simple (e.g., Linear for Non-linear) | Model is too complex (e.g., Deep Tree for noisy data) |
| Primary Fix | Increase complexity, add features | Regularization (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
Interview Questions on This Topic
- QHow do you distinguish between high bias and high variance using a learning curve?Reveal
- QWhat is the 'Double Descent' phenomenon in modern Deep Learning?Reveal
- QExplain the role of Early Stopping in preventing overfitting.Reveal
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.
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.