Confusion Matrix
Confusion Matrix — Break 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)
Threshold controls
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:
- correct predictions (positive and negative).
- 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 spam | predicted not spam | |
|---|---|---|
| actually spam | TP = 3 | FN = 1 |
| actually not spam | FP = 2 | TN = 4 |
The four cells sum to 10 — every prediction lands in exactly one cell.
Step 2 — accuracy: the diagonal over the total
Step 3 — precision: when it says spam, is it right?
Of the 5 emails it flagged, 2 were false alarms.
Step 4 — recall: did it catch the spam?
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?
What is the difference between a false positive and a false negative?
Why is a confusion matrix better than accuracy alone?
How do I read a confusion matrix for more than two classes?
Does the confusion matrix change with the decision threshold?
Written & reviewed by the ML Visualization team · Last updated .