Skip to Content

The Confusion Matrix

When to use

Read a 2x2 table that splits a classifier into correct-vs-incorrect times positive-vs-negative. Foundation of nearly every classification metric.

Analogy

Smoke detector test in a factory. Detector rings or stays quiet; fire is real or a false alarm. Four cells: rang-and-fire (TP), quiet-no-fire (TN), rang-no-fire (FP), quiet-real-fire (FN).

Data-flow diagram

  predicted
         neg   pos
  actual
    neg   TN    FP   <- false alarms
    pos   FN    TP   <- misses

  accuracy = (TN+TP) / total
  precision = TP / (TP+FP)
  recall    = TP / (TP+FN)

Deep explanation

Every classifier prediction falls into one of four cells. Accuracy looks innocent but hides problems on imbalanced data: a fraud model that always predicts NO fraud scores 99 percent accuracy on a 1 percent fraud dataset. Precision and recall isolate one error direction each. Pass labels= explicitly when calling sklearn so the row/column order is unambiguous.

Examples

Example 1

from sklearn.metrics import confusion_matrix
y_true = [0, 1, 0, 1, 1, 0]
y_pred = [1, 1, 0, 0, 1, 0]
tn, fp, fn, tp = confusion_matrix(y_true, y_pred).ravel()
print(f"TN={tn} FP={fp} FN={fn} TP={tp}")

sklearn ravel flattens the 2x2 binary case into TN, FP, FN, TP for downstream formulas.

Example 2

import numpy as np
cm = np.array([[940, 12], [18, 30]])  # TN FP / FN TP
print('accuracy =', np.trace(cm) / cm.sum())
print('precision =', cm[1,1] / cm[:,1].sum())
print('recall    =', cm[1,1] / cm[1,:].sum())

Trace/sum is accuracy; column-sum divides gives precision per class; row-sum gives recall per class.

Example 3

import seaborn as sns, matplotlib.pyplot as plt
cm = confusion_matrix([0,1,2,2,0,1], [0,1,2,0,0,2], labels=[0,1,2])
sns.heatmap(cm, annot=True, fmt='d'); plt.show()

Heatmaps reveal per-class confusion at a glance — vital when you have 10+ classes.

Common mistake

Reading rows and columns in the wrong order silently inverts precision and recall. Always pass labels=[neg, pos] explicitly.

Key takeaway

Confusion matrix first; everything else derives from TN/FP/FN/TP. Pass labels explicitly.

Production Failure Playbook

Failure scenario 1: accuracy-on-imbalanced-data

Failure scenario 2: label-order-inversion