beginnermachine-learning

Confusion Matrix

A simple 2x2 table that shows exactly where your classifier is getting confused.

Key Points

What it is

A confusion matrix is a table that compares your model's predictions to the actual true labels. For binary classification, it has four cells: True Positives (correctly predicted Yes), True Negatives (correctly predicted No), False Positives (predicted Yes but actually No – a 'false alarm'), and False Negatives (predicted No but actually Yes – a 'miss'). From these four numbers, you can calculate accuracy, precision, recall, and many other metrics. It tells you not just how many errors, but what kind of errors.

Why it exists

Accuracy alone hides critical information. A cancer test that always says 'no cancer' is 99% accurate if only 1% of people have cancer. But it misses every single cancer patient – a disaster. The confusion matrix exists because different errors have different costs. A false positive (telling a healthy person they have cancer) causes stress. A false negative (telling a sick person they are healthy) can kill. You need to see both separately. The confusion matrix gives you that view.

What it measures

The confusion matrix measures not just overall performance but the tradeoffs. From it you calculate precision: of all the times you said yes, how many were actually yes? Recall: of all the actual yes cases, how many did you catch? Specificity: of all actual no cases, how many did you correctly reject? False positive rate: of actual no cases, how many did you falsely say yes? Each number tells a different story about your model's behavior.

Intuition

Imagine you are a security guard at an airport. Your job is to flag potential threats. Your model (the metal detector) can beep (predict threat) or not. Confusion matrix: True Positive: beep and actual weapon. True Negative: no beep and no weapon. False Positive: beep but no weapon (someone's belt buckle). False Negative: no beep but actual weapon (bad). The matrix lets you see: are you annoying innocent travelers (many false positives) or are you missing real threats (false negatives)? You cannot optimize both at the same time. The matrix makes this tradeoff explicit.

Formulas

Confusion matrix layout

[TNFPFNTP]\begin{bmatrix} TN & FP \\ FN & TP \end{bmatrix}
  • Top left — True Negatives (predicted No, actual No)
  • Top right — False Positives (predicted Yes, actual No)
  • Bottom left — False Negatives (predicted No, actual Yes)
  • Bottom right — True Positives (predicted Yes, actual Yes)

When to use

When not to use

Common mistakes

In practice

In Python: `from sklearn.metrics import confusion_matrix`. Run `cm = confusion_matrix(y_true, y_pred)`. This returns a 2D array. Plot it with `seaborn.heatmap(cm, annot=True)`. For multiclass, same function works. Use `normalize='true'` to get recall per class, or `normalize='pred'` to get precision per class. Also `ConfusionMatrixDisplay.from_estimator()` is a quick plot. Always look at the matrix before calculating any single-number metric.

Was this concept helpful?

Spot an error or gap? Suggest an edit →

Related concepts

precision-recallroc-aucclassification-threshold
Chirp at me!