Confusion Matrix

What Is a Confusion Matrix?

A confusion matrix is a table used to evaluate classification models. It compares the model's predictions with the true labels.

A Confusion Matrix Shows
  • Correct positive predictions
  • Incorrect positive predictions
  • Correct negative predictions
  • Incorrect negative predictions

It helps explain what kinds of mistakes a model is making.

Why Confusion Matrices Matter

Accuracy only tells us the percentage of correct predictions. A confusion matrix gives more detail by showing the type of each correct and incorrect prediction.

A Confusion Matrix Helps Answer
  • How many positives were correctly found?
  • How many negatives were correctly found?
  • How many false alarms happened?
  • How many positive cases were missed?

Binary Classification

A binary classification problem has two possible classes. Confusion matrices are commonly used for binary classification.

Binary Classification Examples
  • Spam or Not Spam
  • Fraud or Not Fraud
  • Disease or No Disease
  • Pass or Fail
  • Positive or Negative

Positive and Negative Classes

In binary classification, one class is usually called positive and the other is called negative. The meaning depends on the problem.

Disease Detection
  • Positive: Disease Present
  • Negative: No Disease
Spam Detection
  • Positive: Spam
  • Negative: Not Spam

Prediction vs Actual Label

A confusion matrix compares what the model predicted with the correct answer from the dataset.

Example
  • Prediction: Spam
  • Actual Label: Not Spam
  • Result: Incorrect prediction

The confusion matrix shows what type of incorrect prediction it is.

The Four Parts of a Confusion Matrix

A binary confusion matrix contains four main values.

The Four Parts
  • True Positive — Model predicts positive, actual label is positive
  • False Positive — Model predicts positive, actual label is negative
  • True Negative — Model predicts negative, actual label is negative
  • False Negative — Model predicts negative, actual label is positive

True Positives

A true positive happens when the model correctly predicts the positive class. The model found a positive case correctly.

Disease Detection Example
  • Prediction: Disease Present
  • Actual Label: Disease Present
  • Result: True Positive

False Positives

A false positive happens when the model predicts positive, but the actual label is negative. The model raised a false alarm.

Disease Detection Example
  • Prediction: Disease Present
  • Actual Label: No Disease
  • Result: False Positive

True Negatives

A true negative happens when the model correctly predicts the negative class. The model correctly identified a negative case.

Disease Detection Example
  • Prediction: No Disease
  • Actual Label: No Disease
  • Result: True Negative

False Negatives

A false negative happens when the model predicts negative, but the actual label is positive. The model missed a positive case.

Disease Detection Example
  • Prediction: No Disease
  • Actual Label: Disease Present
  • Result: False Negative

Confusion Matrix Layout

A common confusion matrix layout uses actual labels as rows and predicted labels as columns. Always check the labels since tools may display them differently.

Layout
  • Actual Positive + Predicted Positive = True Positive
  • Actual Positive + Predicted Negative = False Negative
  • Actual Negative + Predicted Positive = False Positive
  • Actual Negative + Predicted Negative = True Negative

Confusion Matrix Example

Imagine a disease detection model tested on 100 patients.

Results
  • True Positives: 40 — Sick patients correctly identified
  • False Positives: 10 — Healthy patients incorrectly flagged
  • True Negatives: 45 — Healthy patients correctly identified
  • False Negatives: 5 — Sick patients missed
  • Total: 40 + 10 + 45 + 5 = 100

Confusion Matrix and Accuracy

Accuracy measures the percentage of all correct predictions. Correct predictions are true positives and true negatives.

Formula and Example

Accuracy = (TP + TN) / (TP + FP + TN + FN)

  • TP = 40, TN = 45, FP = 10, FN = 5
  • Accuracy = (40 + 45) / 100 = 85%

Confusion Matrix and Precision

Precision measures how many predicted positives were actually positive. It is useful when false positives are costly.

Formula and Example

Precision = TP / (TP + FP)

  • TP = 40, FP = 10
  • Precision = 40 / (40 + 10) = 80%

Confusion Matrix and Recall

Recall measures how many actual positives were found. It is useful when false negatives are costly.

Formula and Example

Recall = TP / (TP + FN)

  • TP = 40, FN = 5
  • Recall = 40 / (40 + 5) = 88.9%

Confusion Matrix and F1 Score

F1 score balances precision and recall. The confusion matrix provides the values needed to calculate it.

F1 Score

F1 Score = 2 × (Precision × Recall) / (Precision + Recall)

Uses: True positives, false positives, false negatives.

Purpose: Balance precision and recall.

False Positives vs False Negatives

False positives and false negatives are both errors, but they mean different things.

False Positive

The model says something is positive when it is not.

Spam: Important email sent to spam folder.

False Negative

The model says something is negative when it is actually positive.

Spam: Spam email allowed into inbox.

When False Positives Are Costly

False positives are costly when incorrect positive predictions cause harm, frustration, or wasted resources.

Examples
  • Normal email marked as spam
  • Normal transaction blocked as fraud
  • Weak applicant incorrectly recommended
  • Healthy patient incorrectly flagged

Metric often important: Precision

When False Negatives Are Costly

False negatives are costly when missing a real positive case is dangerous or harmful.

Examples
  • Sick patient missed by model
  • Fraud transaction not flagged
  • Dangerous object not detected
  • Cybersecurity attack missed

Metric often important: Recall

Confusion Matrix in Spam Detection

Spam Detection
  • True Positive — Spam email correctly marked as spam
  • False Positive — Normal email incorrectly marked as spam
  • True Negative — Normal email correctly kept in inbox
  • False Negative — Spam email incorrectly allowed into inbox

Helps evaluate whether the spam filter is too strict or too weak.

Confusion Matrix in Medical Diagnosis

Medical Diagnosis
  • True Positive — Patient with disease correctly flagged
  • False Positive — Healthy patient incorrectly flagged
  • True Negative — Healthy patient correctly cleared
  • False Negative — Patient with disease incorrectly missed

Helps show whether the model is missing important cases or creating too many false alarms.

Confusion Matrix in Fraud Detection

Fraud Detection
  • True Positive — Fraud transaction correctly flagged
  • False Positive — Normal transaction incorrectly flagged
  • True Negative — Normal transaction correctly approved
  • False Negative — Fraud transaction incorrectly approved

Helps balance catching fraud with avoiding unnecessary blocked transactions.

Confusion Matrix and Imbalanced Data

A confusion matrix is especially helpful with imbalanced data because it reveals mistakes that accuracy can hide.

Fraud Dataset
  • Normal Transactions: 990
  • Fraud Transactions: 10
  • Model: Predicts everything as normal
  • Accuracy: 99%
  • True Positives: 0, False Negatives: 10

The model missed every fraud case, despite 99% accuracy.

Confusion Matrix for Multi-Class Classification

Confusion matrices can also be used for multi-class classification. They show which classes were confused with each other.

Example Classes: Cat, Dog, Bird
  • Actual Cat predicted as Dog
  • Actual Bird predicted as Cat
  • Actual Dog predicted as Dog correctly

Helps identify which classes are hardest for the model to separate.

Reading a Multi-Class Confusion Matrix

In a multi-class confusion matrix, correct predictions usually appear on the diagonal, while incorrect predictions appear off the diagonal.

Reading the Matrix
  • Rows: Actual classes
  • Columns: Predicted classes
  • Diagonal: Correct predictions
  • Off-diagonal: Incorrect predictions

Example: If many cats are predicted as dogs, the model may struggle to separate cats from dogs.

Common Mistakes

Common Mistakes
  • Confusing false positives and false negatives
  • Looking only at accuracy
  • Ignoring the cost of different errors
  • Not checking class imbalance
  • Misreading rows and columns
  • Assuming all mistakes are equally serious
  • Ignoring precision and recall
  • Not evaluating performance across groups

A confusion matrix is useful only when it is interpreted carefully.

Summary

Key Takeaways
  • A confusion matrix organizes classification results.
  • It compares predicted labels with actual labels.
  • True positives are correctly predicted positives.
  • False positives are incorrectly predicted positives.
  • True negatives are correctly predicted negatives.
  • False negatives are incorrectly predicted negatives.
  • Accuracy, precision, recall, and F1 score can be calculated from a confusion matrix.
  • Confusion matrices reveal mistakes that accuracy alone can hide.
  • False positives and false negatives have different real-world meanings.

Practice Prompt

A disease detection model has 50 true positives, 20 false positives, 80 true negatives, and 10 false negatives.

Identify what each number means. Then calculate accuracy, precision, and recall from the confusion matrix.

Need Help?

Ask the AI if you need help understanding true positives, false positives, true negatives, false negatives, accuracy, precision, recall, or how to read a confusion matrix.