No history yet

Confusion Matrix

Correct, Wrong, and Confused

After a machine learning model is trained, it starts making predictions. But how good are those predictions? Just saying it's "85% accurate" doesn't tell the whole story. We need to know how it's right and how it's wrong. Is it good at identifying one thing but terrible at another?

This is where a tool called the confusion matrix comes in. It's a simple table that gives us a clear picture of a classification model's performance. It shows us, at a glance, where our model is getting things right and where it's getting confused.

The Four Outcomes

Imagine a model designed to detect whether a photo contains a cat. For any given photo, there are two possibilities: it either has a cat or it doesn't. The model will also make one of two predictions: "cat" or "no cat."

This gives us four possible outcomes when we compare the model's prediction to reality. A confusion matrix organizes these four outcomes into a 2x2 grid.

Let's break down each square. We'll define the "positive" class as a photo containing a cat, and the "negative" class as a photo without one.

  • True Positive (TP): The photo has a cat, and the model correctly predicts "cat." This is a correct prediction.
  • True Negative (TN): The photo does not have a cat, and the model correctly predicts "no cat." This is also a correct prediction.
  • False Positive (FP): The photo does not have a cat, but the model incorrectly predicts "cat." This is a mistake, sometimes called a "false alarm" or a Type I error.
  • False Negative (FN): The photo has a cat, but the model incorrectly predicts "no cat." This is also a mistake, often called a "miss" or a Type II error.

In short, if the label starts with "True," the model was right. If it starts with "False," the model was wrong.

Why It Matters

The confusion matrix is more than just a scorecard. It helps us understand the nature of our model's errors, which is crucial. The consequences of a False Positive are not always the same as a False Negative.

Consider a medical test to detect a serious disease. A False Positive (the test says you have the disease, but you don't) would cause stress and lead to more, unnecessary tests. That's bad. But a False Negative (the test says you're fine, but you actually have the disease) could be catastrophic, as a treatable illness might go undetected.

In this scenario, we would be much more concerned about False Negatives than False Positives. By looking at the counts in each quadrant of the confusion matrix, we can see exactly what kind of mistakes our model is making. This allows us to fine-tune the model or decide if it's even suitable for the task.

Lesson image

The matrix itself doesn't give a single score. Instead, it provides the raw data we need to evaluate a model's performance in a nuanced way. It is the foundation for almost all other classification metrics, like accuracy, precision, and recall.