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 21 of the AI/ML Foundations: Core Concepts & First Models course
AI/MLJune 25, 20264 min read

Error Analysis Plots: Diagnosing Model Failures in Python

Master error analysis plots to move beyond aggregate metrics. Learn to visualize residuals for regression and classification errors to find model blind spots.

machine learningerror analysisdata visualizationpythonscikit-learnregressionaimachine-learning

Previously in this course, we discussed Regression Evaluation Metrics and the Confusion Matrix, which provide high-level summaries of how well your model performs. However, aggregate numbers like RMSE or accuracy hide the why behind a failure.

Error analysis is the diagnostic phase of machine learning where we look at individual predictions to understand where the model struggles. If your model is failing, it's rarely failing everywhere equally; it’s usually failing on specific subsets of data. Today, we’ll use error analysis and visualization to uncover those patterns.

Visualizing Regression Residuals

A residual is simply the difference between the actual value and the predicted value: $residual = y_{actual} - y_{predicted}$. If your model were perfect, all residuals would be zero.

A residual plot displays the predicted values on the x-axis and the residuals on the y-axis. In a well-behaved linear model, you want to see a random "cloud" of points centered around zero. If you see a pattern—like a U-shape or a funnel—it means your model is failing to capture a systematic relationship in the data.

Worked Example: Residual Plotting

Using our ongoing project dataset, let’s visualize the residuals to see if our linear model is missing non-linear patterns.

PYTHON
import matplotlib.pyplot as plt
import numpy as np

# Assuming CE9178">'y_test' and CE9178">'y_pred' are your numpy arrays
residuals = y_test - y_pred

plt.figure(figsize=(8, 5))
plt.scatter(y_pred, residuals, alpha=0.5)
plt.axhline(y=0, color=CE9178">'r', linestyle=CE9178">'--')
plt.xlabel("Predicted Values")
plt.ylabel("Residuals")
plt.title("Residual Plot: Checking for Systematic Bias")
plt.show()

If the points form a megaphone shape (getting wider as predictions increase), this indicates heteroscedasticity—your model's error variance is not constant, often because it's failing to account for the increasing scale of your target variable.

Visualizing Classification Errors

For classification, we don't have residuals, but we do have "false" predictions. We want to know: What do the examples the model gets wrong have in common?

If you are building a binary classifier, take your test set and filter for the rows where the model was wrong (False Positives and False Negatives). Then, compare the distribution of features for these "error" rows against the rest of the dataset.

Worked Example: Error Pattern Inspection

PYTHON
# Assuming CE9178">'X_test' is a DataFrame and CE9178">'y_test', CE9178">'y_pred' are arrays
errors = X_test[y_test != y_pred]
correct = X_test[y_test == y_pred]

# Compare the mean of a specific feature for errors vs correct predictions
print(f"Mean of feature CE9178">'Age' in errors: {errors[CE9178">'Age'].mean()}")
print(f"Mean of feature CE9178">'Age' in correct: {correct[CE9178">'Age'].mean()}")

If the mean "Age" is significantly different in your error set, you’ve found a blind spot. Perhaps your model performs poorly on older individuals because they are underrepresented in the training data.

Hands-on Exercise

  1. Take the model you trained in Training the Baseline Linear Model.
  2. Generate a residual plot for your test set.
  3. Identify the 5% of data points with the largest absolute residuals. These are your "worst" predictions.
  4. Print the features of these 5 rows. Do they share a common trait (e.g., all have a specific categorical value)?

Common Pitfalls

  • Ignoring the Scale: If your residuals are massive, check if your target variable needs scaling or if you have extreme outliers. A few massive outliers can skew your entire plot, making it impossible to see the behavior of the majority of your data.
  • Assuming Randomness: Don't assume that because your residual plot looks "okay," the model is done. Always look for clusters. If you see a cluster of points far from the zero line, your model is likely missing a feature that defines that specific cluster.
  • Data Leakage in Analysis: Never perform error analysis on your training set to "tune" your features. You must perform this analysis on your test set (or a dedicated validation set) to ensure you are seeing how the model generalizes to unseen data.

Recap

Aggregate metrics are your starting point, but error analysis is your compass. By plotting residuals, you can spot systematic bias in regression. By isolating and profiling classification errors, you can identify specific segments of your population where the model is failing. Visualization turns abstract loss numbers into actionable insights about your data.

Up next: We will dive into Introduction to Cross-Validation to ensure our error estimates are robust and not just a fluke of a single train-test split.

Previous lessonThe Confusion MatrixNext lesson Introduction to Cross-Validation
Back to Blog

Similar Posts

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.

Read more
AI/MLJune 25, 20263 min read

Advanced Hyperparameter Search: Beyond Grid Search

Master advanced hyperparameter tuning with RandomizedSearchCV and Bayesian optimization. Learn to scale your experiments efficiently for better ML models.

Part of the course

AI/ML Foundations: Core Concepts & First Models

beginner · Lesson 21 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

Model Interpretability Basics: Coefficients and SHAP Explained

Learn how to demystify your models using linear coefficients and SHAP values. Understand why transparency is essential for trust and debugging in production.

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