Confusion Matrix, Precision-Recall & F1
Every classification error has a name and a cell. Dissect the confusion matrix in its 2×2 and 3×3 forms, learn what TP, FP, FN and TN really cost, and see how Precision, Recall and F1 are balanced under severe class imbalance.
What Is a Confusion Matrix?
A confusion matrix is a simple table that cross-tabulates what the model predicted against what was actually true. It is called “confusion” because it shows exactly where the model gets confused — and where it does not.
For a binary problem, the matrix is 2×2. Read it by position: rows are the actual class, columns are the predicted class. The diagonal (top-left, bottom-right) always holds correct predictions; everything off the diagonal is a specific kind of mistake.
Test set: 20 spam emails (the positive class) and 80 legitimate emails. The classifier flagged 23 emails as spam. The matrix on the right records every one of the 100 verdicts.
Two cells will dominate everything that follows: the diagonal correct predictions, and the two error cells mistakes — each with a very different real-world cost.
Rows = Actual Class · Columns = Predicted Class
TP, FP, FN & TN — Highlighted in Both Matrices
Select an outcome below. The corresponding square lights up in the 2×2 binary matrix and in the 3×3 multiclass matrix simultaneously — the same four ideas extend from two classes to many.
True Positive
Predicted Positive · Actually PositiveThe model raised the alarm, and the alarm was real. The prediction and the ground truth agree on the positive class.
Every correct detection. The numerator of both Precision and Recall — the only cell every metric wants to grow.
Reading a 3×3 Matrix Per Class
With classes the matrix becomes . There is no single global TP/FP/FN anymore — instead we compute one binary confusion matrix per class and derive per-class metrics:
Mnemonic: precision sums down the column (everything predicted as this class), recall sums across the row (everything that truly was this class).
Per-class numbers give scores — but leaderboards need one. Three averaging strategies collapse them, each hiding different things:
Unweighted mean over classes. Every class counts equally — best when each class matters equally, regardless of how rare it is.
Pool all counts globally first. Dominated by frequent classes — for single-label multiclass it equals accuracy.
Support-weighted mean. Frequent classes dominate — a compromise that still reports rare-class failures.
Macro recall here = 0.817. If one rare class scored near zero, macro averaging would expose it — micro averaging would bury it.
From Four Counts to Every Metric
Every classification metric is just a different ratio over the same four cells. The question is never “which formula” — it is which error cell hurts your application.
“Of everything I flagged, how much was real?” Punishes FP. Optimize when false alarms are expensive — spam filters, recommendation reranking.
“Of all real positives, how many did I catch?” Punishes FN. Optimize when misses are expensive — cancer screening, fraud detection.
“How often am I right overall?” Collapses under imbalance — 95% negatives make the trivial “always negative” model 95% accurate.
“How well do I leave negatives alone?” The mirror of recall — the two together form the ROC curve of Topic 04.
Precision–Recall Trade-off Simulator
In fraud detection, rare pathology, or cyber threats, positive events occur in fewer than 5% of cases. A naive model predicting “all negative” achieves 95% accuracy while finding zero actual threats.
Drag down: recall climbs, precision collapses into false alarms. Drag it up: precision sharpens, recall starves. Accuracy barely notices — watch how static it stays while the errors swap places. That numbness is the paradox.
Lower τ slides the operating point right into the precision cliff · Higher τ pushes it up-left
F1 & the Generalized F-Beta
The arithmetic mean of precision and recall is — concealing total failure. The harmonic mean heavily penalizes asymmetry: whenever one side collapses, F1 collapses with it.
Because can be rewritten purely in confusion-matrix counts, it never touches TN — under extreme imbalance it stays honest where accuracy cannot.
Real applications are rarely symmetric. When missed detections matter times more than false alarms, the generalized score shifts the balance:
β = 1 is plain F1 — precision and recall weighted equally, the neutral harmonic compromise.
Same Four Cells, A Different Curve
Nothing here is new data. The same four cells can be plotted in two coordinate systems, and each produces its own curve. Slide a single decision threshold across the score axis: every position yields one confusion matrix, and every confusion matrix yields one point. Connect the points and you have plotted recall against precision — the curve of this article — or, if you prefer, TPR against FPR, which is the ROC curve of Topic 04. The axes differ; the ledger underneath does not.
The two axes are the same numbers wearing different labels. and . Both curves sweep every threshold, so both are threshold-invariant in the sense of summarising the full range without committing to one operating point.
They part ways on prevalence. TPR and FPR each normalise inside their own class — TPR divides by all the real positives, FPR by all the real negatives — so the ROC curve is unchanged if you keep the model fixed and make the positive class a hundred times rarer. Precision refuses that trick: it divides by , which contains the negative class through the false alarms. Change the class balance and the PR curve moves even though the model did not.
Recall = 15/20 = 0.750
FPR = 8/80 = 0.100
Precision = 15/23 = 0.652
Recall = 15/20 = 0.750
FPR = 8/9,980 ≈ 0.0008
Precision = 15/23 = 0.652
The false alarms did not change; only the pool of legitimate mail did. ROC's FPR collapses toward zero and the point climbs into the top-left corner, flattered by a denominator it never had to earn. Precision is unmoved at 65.2% — it has no TN term to hide behind. When positives are rare, the PR curve is the honest view.
At τ = 0.50 the sweep sits exactly on the running example · R 75% · P 65.2%
| Metric | Cells Used | Blind To | Answers |
|---|---|---|---|
| Accuracy | (TP + TN) / N | the FP / FN split | How often am I right overall? |
| Precision | TP / (TP + FP) | FN · TN | How much of what I flagged is real? |
| Recall · TPR | TP / (TP + FN) | FP · TN | How many real positives did I catch? |
| Specificity | TN / (TN + FP) | FN · TP | How well do I leave negatives alone? |
| FPR | FP / (FP + TN) | FN · TP | How often do negatives get flagged? |
| F1 | 2TP / (2TP + FP + FN) | TN | A single harmonic compromise. |
| Fβ | weighted P & R | TN | A cost-weighted compromise. |
| ROC curve | TPR vs FPR | the operating threshold | Every threshold, at once. |
| AUC | ∫ TPR d(FPR) | threshold · calibration | How well does it rank? |
| PR curve | Precision vs Recall | TN · prevalence | Ranking quality when positives are rare. |
