Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 73

Understanding Confusion Matrices

Recommended resources

Hello! Welcome to the next lesson in your journey through supervised learning.

In our last lesson on Support Vector Machines, you saw how we used a couple of key tools from scikit-learn—classification_report and confusion_matrix—to evaluate our final, tuned model. We printed the matrix, but we didn't stop to examine what it truly represents. Today, we're going to fix that.

This lesson is dedicated to mastering one of the most fundamental and insightful tools for evaluating a classification model. By the end of this hour, you will be able to construct and interpret a confusion matrix for model evaluation, for both binary and multiclass problems. This will move you from simply generating model outputs to deeply understanding your model's specific strengths and weaknesses.

What is a Confusion Matrix?

At its core, a confusion matrix is a simple table that summarizes the performance of a classification algorithm. It's called a "confusion" matrix because it shows you exactly where your model is getting "confused" when it makes predictions.

For a binary classification problem (e.g., "spam" or "not spam"), the matrix is a 2x2 table that breaks down the predictions into four distinct categories.

To get a clear and friendly introduction to this concept, let's turn to StatQuest.

Machine Learning Fundamentals: The Confusion Matrix

The video 'Machine Learning Fundamentals: The Confusion Matrix' provides an excellent conceptual breakdown. Watch the first part to understand the four fundamental components of the matrix.

Watch the segment from 01:21 to 02:45. Focus on understanding the definitions of True Positives, True Negatives, False Positives, and False Negatives using the heart disease example.

As the video explained, the four cells of the matrix are:

  • True Positives (TP): The model correctly predicted the positive class. (e.g., correctly identified a spam email as "spam").
  • True Negatives (TN): The model correctly predicted the negative class. (e.g., correctly identified a regular email as "not spam").
  • False Positives (FP): The model incorrectly predicted the positive class. This is also known as a Type I Error. (e.g., a regular email was incorrectly flagged as "spam").
  • False Negatives (FN): The model incorrectly predicted the negative class. This is also known as a Type II Error. (e.g., a spam email was missed and allowed into the inbox, labeled "not spam").

Here is the standard layout. The main diagonal (top-left to bottom-right) shows the correct predictions, while the off-diagonal shows the errors.

Confusion Matrix Explained
A standard 2x2 confusion matrix layout, showing the positions of True Positives (TP), False Negatives (FN), False Positives (FP), and True Negatives (TN). It also shows the formulas for Recall and Precision, which are metrics derived from these values.

Interpreting the Matrix: Why the Type of Error Matters

A confusion matrix is far more useful than a single accuracy score because it tells you what kind of errors your model is making. In many real-world scenarios, the cost of one type of error is much higher than the other.

Consider two scenarios:

  1. Email Spam Filter: A False Positive means an important email (e.g., a job offer) goes to the spam folder. This is highly undesirable. A False Negative means you see a spam email in your inbox, which is merely an annoyance. Here, minimizing FPs is the priority.
  2. Cancer Screening Test: A False Positive means a healthy patient is told they might have cancer, leading to more tests and anxiety. This is not ideal, but it's manageable. A False Negative means a patient with cancer is told they are healthy, causing them to miss critical, early treatment. This is a catastrophic error. Here, minimizing FNs is paramount.

This context is crucial for model evaluation. The article below expands on this idea and discusses why relying on accuracy alone can be misleading, especially with imbalanced datasets.

What is A Confusion Matrix in Machine Learning ...

The DataCamp article 'What is A Confusion Matrix in Machine Learning' clearly explains the limitations of accuracy and the benefits of analyzing the types of errors your model makes.

Read the section titled 'The Role of a Confusion Matrix', focusing on the subsections 'Limitations of accuracy as a standalone metric' and 'The benefits of a confusion matrix'.

Test your understanding!

You are building a machine learning model for a bank to detect fraudulent credit card transactions. The "positive" class is fraud and the "negative" class is not_fraud. Which type of error would be more costly for the bank and its customers?

  1. False Positive (a legitimate transaction is flagged as fraud)
  2. False Negative (a fraudulent transaction is missed and allowed to go through)
Show answer

The correct answer is 2. False Negative.

A False Negative means the model fails to detect a real fraudulent transaction. This results in a direct financial loss for the customer or the bank.

A False Positive, while inconvenient (the customer's card might be temporarily blocked), does not result in a loss of money and can usually be resolved with a quick phone call. Therefore, in fraud detection, minimizing False Negatives is the top priority.


Constructing a Confusion Matrix in Python

Now let's move from theory to practice. scikit-learn makes it incredibly easy to generate and visualize a confusion matrix. The following video provides a complete walkthrough, from loading data to training a model and plotting the final matrix. Since you're already familiar with the model training workflow, you can focus on the final steps.

Confusion to Clarity: Mastering Confusion Matrix in Machine Learning

The IBM Technology video 'Confusion to Clarity' demonstrates the practical steps to create and interpret a confusion matrix using scikit-learn.

Watch the segments from 08:02 to 12:34. The first part (from 08:02) shows how to generate the matrix as a NumPy array and then as a plot using confusion_matrix and ConfusionMatrixDisplay. The second part (from 10:15) clearly explains how to interpret the numbers in each quadrant of the resulting plot.

Let's consolidate what you've just seen into a clear code example. We'll use the same structure as our previous lessons: create synthetic data, train a model (we'll use a LogisticRegression for simplicity), and then evaluate it.

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.datasets import make_classification
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay

# 1. Create a synthetic dataset
X, y = make_classification(n_samples=1000, n_features=10, n_classes=2, random_state=42)

# 2. Split into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)

# 3. Train a simple model
model = LogisticRegression(random_state=42)
model.fit(X_train, y_train)

# 4. Make predictions
y_pred = model.predict(X_test)

# 5. Generate the confusion matrix
cm = confusion_matrix(y_test, y_pred)

print("Confusion Matrix (as a NumPy array):")
print(cm)

# 6. Visualize the confusion matrix
# It's conventional to have 'True label' on the y-axis and 'Predicted label' on the x-axis
disp = ConfusionMatrixDisplay(confusion_matrix=cm, display_labels=model.classes_)
disp.plot(cmap=plt.cm.Blues)
plt.title('Confusion Matrix')
plt.show()

When you run this code, the confusion_matrix function will return a 2x2 NumPy array. For example:

[[118,  29],
 [ 19, 134]]

Interpreting this array:

  • Top-Left (118): True Negatives (TN). The model correctly predicted class 0, 118 times.
  • Top-Right (29): False Positives (FP). The model incorrectly predicted class 1 when it was actually class 0, 29 times.
  • Bottom-Left (19): False Negatives (FN). The model incorrectly predicted class 0 when it was actually class 1, 19 times.
  • Bottom-Right (134): True Positives (TP). The model correctly predicted class 1, 134 times.

The plot generated by ConfusionMatrixDisplay presents this same information in a more readable, graphical format.

Confusion Matrix Example
An example of a confusion matrix visualized as a heatmap. The axes represent the actual and predicted classes, and the color intensity in each cell corresponds to the number of samples.

Extending to Multiclass Classification

The concept of a confusion matrix extends seamlessly to problems with more than two classes. If you have N classes, you get an N x N matrix.

  • The diagonal elements still represent the number of points for which the predicted label is equal to the true label (i.e., correct classifications).
  • The off-diagonal elements represent the misclassifications. The value in row i, column j is the number of times a sample of true class i was incorrectly predicted as class j.

This allows you to see not just that your model made an error, but also which classes are being confused with each other.

Machine Learning Fundamentals: The Confusion Matrix

Let's return to the StatQuest video to see how the matrix scales up for multiclass problems.

Watch the segment from 04:44 to 06:41. It clearly explains how a 2x2 matrix becomes a 3x3 or NxN matrix and how to interpret the diagonal and off-diagonal elements.

The GeeksForGeeks article below provides a concrete example and the corresponding Python code for a multiclass scenario.

Understanding the Confusion Matrix in Machine Learning

The article 'Understanding the Confusion Matrix in Machine Learning' gives a practical code implementation for a multiclass problem.

Read the sections 'Confusion Matrix For Multi-class Classification' and 'Implementation of Confusion Matrix for Multi-Class classification using Python'. Focus on the 3x3 table example and how the ConfusionMatrixDisplay function is used to plot it.

By examining the off-diagonal elements in a multiclass confusion matrix, you can gain valuable insights. For instance, in an animal image classifier, you might find that the model frequently confuses "cats" with "leopards," but rarely confuses "cats" with "turtles." This specific feedback can guide you on how to improve your model, perhaps by adding more training data that highlights the subtle differences between cats and leopards.


Conclusion

You now have a solid understanding of one of the most important evaluation tools in classification. A confusion matrix goes beyond a simple accuracy score, giving you a detailed breakdown of your model's performance.

Key Takeaways:

  • A confusion matrix summarizes a classifier's performance by comparing actual and predicted labels.
  • It is composed of four key values for binary classification: True Positives (TP), True Negatives (TN), False Positives (FP), and False Negatives (FN).
  • The diagonal of the matrix shows correct predictions, while the off-diagonals reveal the errors.
  • Understanding the context and cost of different error types (FP vs. FN) is critical for proper model evaluation.
  • The concept extends to an N x N matrix for multiclass problems, showing which specific classes are being confused with each other.
  • scikit-learn provides easy-to-use functions (confusion_matrix, ConfusionMatrixDisplay) to construct and visualize the matrix.

Preview of the Next Lesson:

The confusion matrix provides the raw numbers for evaluating a classifier. In the next lesson, we will build upon this by exploring metrics that are calculated from these numbers. We will learn how to plot and interpret Receiver Operating Characteristic (ROC) curves and calculate the Area Under the Curve (AUC). This will provide another powerful visual method to assess and compare the performance of different classification models.