Skip to content
ML Visualization

Confusion Matrix

Data Prep & Model EvaluationBeginner~6 min

Confusion MatrixBreak predictions into true/false positives and negatives.

Accuracy hides too much. A confusion matrix lays out exactly how a classifier succeeds and fails — true positives, false positives, true negatives, false negatives — the raw material for every other metric.

  • Correct
  • Wrong
  • Your threshold
  • Cost-optimal

Expected cost across every threshold (click to jump)

01

Threshold controls

Data
Scenario

Half the cases are positive and both mistakes cost the same — the textbook setting where 0.5 is defensible.

50%
0.60
Model
Pred +Pred −Actual +Actual −

Hover any cell to light up exactly those points on the strip.

1.00
Expected cost197
Cheapest at t0.4749
Playback
What the transport walks
Step 0 / 100
Speed
  1. Strict (t → 1)
  2. Balanced
  3. Permissive (t → 0)

Step 0 of 100 — t = 1.00 — TP 3, FP 0, FN 197, TN 200 · accuracy 51%, recall 2%, expected cost 197

Break it

The idea in plain words

Accuracy hides too much. A confusion matrix lays out exactly how a classifier succeeds and fails: true positives, false positives, true negatives, false negatives. Drag the threshold and watch the four cells trade off, linked to the colored points.

It’s the raw material for every other metric — precision and recall, the ROC curve. And on imbalanced data it exposes the accuracy paradox: predict all-negative, score “99% accurate,” and still be useless.

Now, the math

Accuracy is just the diagonal of the matrix over the total:

accuracy=TP+TNTP+TN+FP+FN\text{accuracy} = \frac{TP + TN}{TP + TN + FP + FN}
TP, TNTP,\ TN
correct predictions (positive and negative).
FP, FNFP,\ FN
the two error types — false alarms and misses.
Show the derivation

When one class is rare, TN dominates the sum, so accuracy stays high even if the model never catches a single positive. That’s why the individual cells — and metrics derived from them — matter more than accuracy alone on imbalanced problems.

Trace it by hand

A spam filter scores 10 emails. Emails 1–4 are truly spam, emails 5–10 are not. The model flags emails 1, 2, 3, 5, and 6 as spam and passes the rest. All fractions below are exact.

Step 1 — count the four cells

Emails 1, 2, 3 are spam and flagged (TP). Email 4 is spam but slipped through (FN). Emails 5 and 6 are clean but flagged (FP). Emails 7–10 are clean and passed (TN):

predicted spampredicted not spam
actually spamTP = 3FN = 1
actually not spamFP = 2TN = 4

The four cells sum to 10 — every prediction lands in exactly one cell.

Step 2 — accuracy: the diagonal over the total

accuracy=TP+TN10=3+410=0.70\text{accuracy} = \frac{TP + TN}{10} = \frac{3 + 4}{10} = 0.70

Step 3 — precision: when it says spam, is it right?

precision=TPTP+FP=33+2=0.60\text{precision} = \frac{TP}{TP + FP} = \frac{3}{3 + 2} = 0.60

Of the 5 emails it flagged, 2 were false alarms.

Step 4 — recall: did it catch the spam?

recall=TPTP+FN=33+1=0.75\text{recall} = \frac{TP}{TP + FN} = \frac{3}{3 + 1} = 0.75

One of the 4 real spam emails got through. Precision and recall are just these two different denominators over the same matrix.

What just happened: one matrix produced three different verdicts — 0.70 accuracy, 0.60 precision, 0.75 recall — because each metric divides different cells. And accuracy is the easiest to fool: on a 99-to-1 imbalanced inbox, predicting “never spam” scores 0.99 accuracy with a recall of exactly 0.

Now Break It

Try this: On imbalanced data, high accuracy hides that the model never catches the rare class.

Control: Class balance slider (set to highly imbalanced)

What happens: Accuracy paradox! 99% accurate but it never catches the rare class — the matrix reveals the truth.

Where confusion matrix is used

A confusion matrix breaks a classifier's predictions into four cells: true positives, true negatives, false positives, and false negatives. This is far more informative than a single accuracy number because it shows exactly what kind of errors the model makes. In medical screening, a false negative means a sick patient is told they are healthy, which is often far more costly than a false positive that triggers a follow-up test. In email spam filtering the priorities flip: a false positive that hides a legitimate message in the spam folder can be worse than letting an occasional spam email through. Nearly every downstream metric, including precision, recall, specificity, and F1, is computed directly from the four counts in this matrix.

The biggest misconception is trusting accuracy on imbalanced data, which the confusion matrix immediately exposes. If one in a thousand transactions is fraudulent, a model that predicts not fraud every time scores 99.9 percent accuracy while catching zero fraud; the matrix reveals an entire empty true-positive cell. Another common confusion is mixing up the two error types. A false positive is a false alarm, predicting positive when the truth is negative, while a false negative is a miss, predicting negative when the truth is positive. Also remember the matrix depends on the decision threshold; the same model produces different cell counts at different cutoffs, so a confusion matrix always describes one specific operating point.

Frequently asked questions

What do the four cells of a confusion matrix mean?
True positives are correctly predicted positives and true negatives are correctly predicted negatives. False positives are negatives wrongly flagged as positive, also called false alarms, and false negatives are positives the model missed. Every classification metric such as precision and recall is built from these four counts.
What is the difference between a false positive and a false negative?
A false positive predicts the positive class when the true label is negative, like flagging a healthy patient as sick. A false negative predicts negative when the truth is positive, like missing a real disease. Which one is worse depends entirely on the application and its consequences.
Why is a confusion matrix better than accuracy alone?
Accuracy collapses all outcomes into one number and can hide serious failures, especially on imbalanced data. The confusion matrix shows the specific counts of each error type, so you can see whether the model is missing positives, raising false alarms, or both, and choose metrics that reflect your real costs.
How do I read a confusion matrix for more than two classes?
For multiclass problems the matrix is a square grid where rows are true classes and columns are predicted classes. Diagonal cells are correct predictions and off-diagonal cells show which classes get confused with which, helping you spot specific pairs the model struggles to distinguish.
Does the confusion matrix change with the decision threshold?
Yes. The matrix reflects one specific threshold used to convert scores into class labels. Lowering the threshold catches more positives but raises false positives, shifting the counts, so any confusion matrix describes a single chosen operating point rather than the model in general.

Written & reviewed by the ML Visualization team · Last updated .