Mahamudul Hasan Rubel
HomeAboutProjectsSkillsExperienceBlogCoursesPhotosContact
Mahamudul Hasan Rubel

Senior Software Engineer crafting high-performance web applications and SaaS platforms.

Navigation

  • Home
  • About
  • Projects
  • Skills
  • Experience
  • Blog
  • Courses
  • Photos
  • Contact

Get in Touch

Available for senior/lead roles and consulting.

bd.mhrubel@gmail.comHire Me

© 2026 Mahamudul Hasan Rubel. All rights reserved.

Built with using Next.js 16 & Tailwind v4

Back to Blog
Lesson 18 of the AI/ML Foundations: Core Concepts & First Models course
AI/MLJune 25, 20264 min read

Overfitting and Underfitting: Bias, Variance, and Model Health

Learn to diagnose overfitting and underfitting by mastering the concepts of bias and variance. Build models that generalize, not just memorize.

machine learningbiasvarianceoverfittingunderfittingdata sciencemodel evaluationaimachine-learningpython

Previously in this course, we explored training error vs generalization error. While that lesson taught you how to compare performance metrics, it didn't explain why those gaps appear. In this lesson, we dive into the root causes: the relationship between model complexity, bias, and variance.

Understanding Bias and Variance from First Principles

Every machine learning model you build is an attempt to map input features to a target outcome. However, no model is perfect. The error a model makes on unseen data can be decomposed into three parts: bias, variance, and irreducible noise.

1. Bias: The Error of Simplicity

Bias measures how much a model's average prediction differs from the true value. A model with high bias makes strong, simplifying assumptions about the data.

  • The symptom: The model ignores relevant patterns.
  • The result: It performs poorly on both the training set and the test set. We call this underfitting. It’s like trying to model a complex, winding mountain road with a single straight line.

2. Variance: The Error of Complexity

Variance measures how much the model's predictions change if you train it on a different subset of the same data. A model with high variance is overly sensitive to the specific noise or "quirks" in your training set.

  • The symptom: The model "memorizes" the training data rather than learning the underlying pattern.
  • The result: It performs exceptionally well on the training data but fails miserably on the test set. We call this overfitting. It’s like trying to model that same mountain road by drawing a jagged line that hits every single pebble on the path.

Identifying the Signs: A Diagnostic Framework

To diagnose your model, you must compare performance metrics across your training and testing sets.

ConditionTraining ErrorTest ErrorComplexity
UnderfittingHighHighToo Low
OverfittingLowHighToo High
Ideal ModelLowLowBalanced

Worked Example: Spotting the Gap

Imagine we are predicting house prices. We use a polynomial regression model.

PYTHON
from sklearn.preprocessing import PolynomialFeatures
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error

# Low complexity: Degree 1 (Underfitting)
# High complexity: Degree 15 (Overfitting)
poly = PolynomialFeatures(degree=15)
X_poly = poly.fit_transform(X_train)

model = LinearRegression()
model.fit(X_poly, y_train)

# Assessing the gap
train_preds = model.predict(X_poly)
test_preds = model.predict(poly.transform(X_test))

print(f"Train MSE: {mean_squared_error(y_train, train_preds)}")
print(f"Test MSE: {mean_squared_error(y_test, test_preds)}")

If your Train MSE is 100 but your Test MSE is 50,000, you are looking at a classic case of overfitting. The model has chased the noise in the training data, losing its ability to generalize. Conversely, if both are 20,000, your model hasn't captured enough signal—that is underfitting.

Hands-on Exercise

Take the baseline model you created in our previous session on training the baseline linear model.

  1. Calculate the MSE for both your training set and your test set.
  2. If the training MSE is significantly lower than the test MSE, your model is likely overfitting. What is one feature you could remove to simplify the model?
  3. If both MSE values are high, your model is likely underfitting. What is one feature you could add or transform (e.g., squaring a value) to help the model capture more signal?

Common Pitfalls

  • Confusing high bias with high variance: Remember, high bias = "I don't know enough," high variance = "I'm obsessed with these specific examples."
  • Ignoring the noise: Sometimes, the gap between training and test error is just irreducible noise (inherent randomness in the data). You cannot eliminate this; don't spend weeks trying to "fix" a model that has hit the theoretical performance ceiling of the dataset.
  • Over-tuning early: Don't jump to complex models (like deep neural networks) if a simple linear model hasn't been properly tuned first. Start simple, then add complexity only when the data demands it.

Recap

Overfitting and underfitting represent the two ends of the model complexity spectrum. By monitoring the performance gap between training and testing data, you can diagnose whether your model suffers from high bias (underfitting) or high variance (overfitting). Your goal is to find the "sweet spot" where the model is complex enough to capture the signal but simple enough to ignore the noise.

Up next: We will learn how to quantify these errors using specific Regression Evaluation Metrics like RMSE and R-squared to make your diagnostic process more precise.

Previous lessonTraining Error vs Generalization ErrorNext lesson Regression Evaluation Metrics
Back to Blog

Similar Posts

AI/MLJune 25, 20264 min read

Evaluating Model Calibration: Accuracy Beyond Just Predictions

Learn how to evaluate model calibration using calibration curves and the Brier score. Ensure your predicted probabilities are accurate representations of reality.

Read more
AI/MLJune 25, 20264 min read

Mastering Regression Evaluation Metrics: RMSE, MAE, and R-squared

Learn to measure model accuracy with essential regression metrics. We break down RMSE, MAE, and R-squared so you can evaluate your predictions like a pro.

Part of the course

AI/ML Foundations: Core Concepts & First Models

beginner · Lesson 18 of 50

  1. 1

    The Machine Learning Workflow

    4 min
  2. 2

    Setting Up the Python ML Environment

    4 min
  3. 3

    Introduction to NumPy for Data Handling

    4 min
Read more
AI/MLJune 25, 20264 min read

Training Error vs Generalization Error: A Practical Guide

Learn why high training performance often masks poor real-world results. Discover how to compare training and testing error to master model generalization.

Read more
4

Loading and Inspecting Datasets with Pandas

3 min
  • 5

    Exploratory Data Analysis Fundamentals

    3 min
  • 6

    Handling Missing and Inconsistent Data

    3 min
  • 7

    Feature Selection and Basic Filtering

    3 min
  • 8

    Project Dataset Initialization

    3 min
  • 9

    Mechanics of Linear Regression

    4 min
  • 10

    Mechanics of Classification

    4 min
  • 11

    Loss Functions and Model Objectives

    4 min
  • 12

    Training and Testing Data Splits

    3 min
  • 13

    Data Scaling Techniques

    4 min
  • 14

    Encoding Categorical Variables

    3 min
  • 15

    Building Scikit-Learn Pipelines

    4 min
  • 16

    Training the Baseline Linear Model

    3 min
  • 17

    Training Error vs Generalization Error

    4 min
  • 18

    Overfitting and Underfitting

    4 min
  • 19

    Regression Evaluation Metrics

    4 min
  • 20

    The Confusion Matrix

    3 min
  • 21

    Error Analysis Plots

    4 min
  • 22

    Introduction to Cross-Validation

    4 min
  • 23

    Diagnosing Model Weaknesses

    3 min
  • 24

    Feature Engineering Strategies

    4 min
  • 25

    Handling Outliers

    3 min
  • 26

    The Bias-Variance Tradeoff

    3 min
  • 27

    Hyperparameter Tuning Basics

    4 min
  • 28

    Implementing Grid Search

    3 min
  • 29

    Refining the Project Model

    3 min
  • 30

    Evaluating Feature Importance

    3 min
  • 31

    Advanced Feature Transformation

    3 min
  • 32

    Regularization Techniques

    3 min
  • 33

    Comparing Different Algorithms

    3 min
  • 34

    Managing Model Complexity

    4 min
  • 35

    Understanding Data Drift

    4 min
  • 36

    Version Control for ML Experiments

    3 min
  • 37

    Exporting Trained Models

    3 min
  • 38

    Creating an Inference Script

    3 min
  • 39

    Building a Simple Web Interface

    3 min
  • 40

    Documenting ML Projects

    4 min
  • 41

    Final Project Review

    4 min
  • 42

    Ensemble Methods Overview

    4 min
  • 43

    Feature Selection via Recursive Elimination

    3 min
  • 44

    Model Interpretability Basics

    4 min
  • 45

    Dealing with High Cardinality

    3 min
  • 46

    Handling Multi-Collinearity

    4 min
  • 47

    Introduction to Pipelines with Custom Transformers

    3 min
  • 48

    Evaluating Model Calibration

    4 min
  • 49

    Advanced Hyperparameter Search

    3 min
  • 50

    Model Monitoring in Practice

    4 min
  • View full course