Sitemap
KVoid

KVoid

Confusion Matrix

3 min readDec 31, 2025

--

Project Overview and Objectives

This project demonstrates the use of a Confusion Matrix, one of the most important evaluation tools used in classification problems in machine learning. Using Python and the scikit-learn library, a confusion matrix is constructed and visualized to evaluate how well a model’s predictions match the actual labels.

The objectives of this project are:

  • To understand what a confusion matrix is and why it is used
  • To learn how classification errors are categorized
  • To implement and visualize a confusion matrix using Python
  • To interpret the results clearly using a real example

Motivation and Real-World Relevance

In real-world classification tasks, accuracy alone is often misleading.

Consider these questions:

  • Is it worse to mark a sick patient as healthy or a healthy patient as sick?
  • Is missing a spam email worse than marking an important email as spam?
  • Is failing to detect fraud worse than flagging a normal transaction?

A confusion matrix answers these questions by showing exactly how predictions fail, not just how often.

Get KVoid’s stories in your inbox

Join Medium for free to get updates from this writer.

It is widely used in:

  • Medical diagnosis
  • Fraud detection
  • Spam filtering
  • Face and object recognition
  • Any binary or multi-class classification system

Basic Theory of the Confusion Matrix

A confusion matrix is a table that compares:

  • Actual labels (ground truth)
  • Predicted labels (model output)

For binary classification, it consists of four key components:

  • True Positive (TP): Correctly predicted positive cases
  • True Negative (TN): Correctly predicted negative cases
  • False Positive (FP): Incorrectly predicted positive cases
  • False Negative (FN): Incorrectly predicted negative cases

These values form the foundation for evaluation metrics such as:

  • Accuracy
  • Precision
  • Recall
  • F1-score

Without a confusion matrix, these metrics have no context.

Python Concepts and Libraries Used

This project uses the following tools:

  • NumPy — numerical arrays and data handling
  • Seaborn — statistical data visualization
  • Matplotlib — plotting and figure customization
  • scikit-learn — machine learning evaluation tools
  • confusion_matrix — core evaluation function

Step-by-Step Code Explanation (Line by Line)

Importing Required Libraries

import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix

Explanation:

  • numpy is used to store actual and predicted labels
  • seaborn creates visually appealing heatmaps
  • matplotlib controls plot labels and titles
  • confusion_matrix computes the confusion matrix value

Defining Actual and Predicted Labels

actual = np.array(['dog', 'dog', 'dog', 'not dog', 'dog', 'not dog', 'dog', 'dog', 'not dog', 'not dog'])
predict = np.array(['dog', 'not dog', 'dog', 'not dog', 'dog', 'dog', 'dog', 'dog', 'not dog', 'not dog'])

Here:

  • actual represents the true labels
  • predict represents the model’s predictions
  • Each index corresponds to one classification decision

This setup simulates a binary classification problem.

Generating the Confusion Matrix

w = confusion_matrix(actual, predict)

This line:

  • Compares actual labels with predicted labels
  • Counts correct and incorrect predictions
  • Returns a 2×2 matrix for binary classification

The matrix structure is:

[[True Negatives, False Positives],
[False Negatives, True Positives]]

Defining Class Labels

labels = ['dog', 'not dog']

These labels:

  • Improve readability of the visualization
  • Help interpret which class corresponds to which axis

Visualizing the Confusion Matrix

sns.heatmap(w,
annot=True,
fmt='g',
cmap='Oranges',
xticklabels=labels,
yticklabels=labels)

Explanation:

  • heatmap visually represents the confusion matrix
  • annot=True displays numeric values inside cells
  • fmt='g' ensures clean number formatting
  • cmap='Oranges' enhances contrast
  • Axis labels show predicted vs actual classes

Adding Titles and Axis Labels

plt.title('Confusion Matrix')
plt.xlabel('Predicted')
plt.ylabel('Actual')
plt.show()

This step:

  • Clearly labels the axes
  • Improves interpretability
  • Displays the final visualization

Visualization is crucial because humans interpret patterns far faster visually than numerically.

Output Of The Program

Key Learning Outcomes

  • A confusion matrix reveals how predictions fail
  • Accuracy alone is not enough for evaluation
  • False positives and false negatives matter differently
  • Visualization improves interpretability
  • Confusion matrices are foundational to classification metrics

Conclusion

This project demonstrates how a confusion matrix provides deep insight into the performance of a classification model. By breaking predictions into correct and incorrect categories, it allows practitioners to understand not just how often a model is right, but how it is wrong.

The confusion matrix is not optional — it is essential. Any serious classification system relies on it to make informed, responsible decisions, especially in high-stakes real-world applications.

--

--