Confusion matrix

A confusion matrix is a simple way to count what a classifier got right and wrong. For a spam filter, each email is either actually spam or actually not spam, and the filter predicts spam or not spam. That gives four outcomes:

From these counts you get two useful measures.

Precision asks: when the filter says "spam," how often is it right? Precision = TP / (TP + FP). High precision means few false alarms, so you rarely lose real email to the junk folder.

Recall asks: out of all the spam that exists, how much did the filter catch? Recall = TP / (TP + FN). High recall means little spam slips through.

Most filters output a score, say 0 to 1, and you pick a threshold above which an email is marked spam. Raise the threshold and you flag fewer emails: false positives drop, so precision tends to rise, though not at every step; watch it dip as the slider moves. False negatives climb, so recall falls, and recall only ever falls as the threshold rises. Lower it and the reverse happens.

There is no setting that makes both perfect when spam and real mail overlap in score. Choosing the threshold means choosing which mistake you can live with. A spam filter would rather let some spam through than bury your real mail. A cancer screen makes the opposite choice: catch every real case, even at the cost of false alarms.

Choosing a threshold

Classification

Precision and recall at a decision threshold

predicted −predicted +actual +actual −00.250.50.751model score
correctwrong
predicted +
predicted −
actual +
16
TP
8
FN
actual −
5
FP
21
TN
threshold
0.50
precision
0.762
recall
0.667
predicted +
21

The 50 emails are synthetic: a seeded generator draws two overlapping score distributions, so the dataset is identical on every visit. Every count in the matrix, and every precision and recall value, is computed live from those points as the threshold moves.