Sub topic 2
The Four Outcomes: TP, FP, FN, TN
This chapter assumes you know what supervised binary classification is, can read short Python snippets, and are comfortable with counts and ratios. It covers the four outcome types that every later metric is built from.
Two labels for every example
A binary classifier assigns each example a predicted label. Each example also has an actual label, the ground truth. By convention one class is called positive (usually the one you want to find, such as "fraud" or "disease") and the other negative. Which class counts as positive is a choice you make, and it changes how the outcomes are named.
Comparing the predicted label with the actual label gives two questions per example:
- Did the model predict positive or negative?
- Was the prediction correct?
The four combinations of answers are the four outcomes.
The outcomes
| Outcome | Actual | Predicted | Meaning |
|---|---|---|---|
| True positive (TP) | positive | positive | A real positive was found. |
| False positive (FP) | negative | positive | A false alarm. |
| False negative (FN) | positive | negative | A real positive was missed. |
| True negative (TN) | negative | negative | A real negative was correctly left alone. |
Each name has two parts. The second word is what the model predicted: positive or negative. The first word says whether that prediction was true (correct) or false (wrong). So a "false negative" is a wrong negative prediction, which means the example was actually positive.
Every example lands in exactly one cell. The four counts therefore add up to the number of examples, and TP + FN is the number of actual positives.
Counting them in code
This snippet counts the outcomes by comparing each pair of labels.
y_true = [1, 0, 1, 1, 0, 0, 1, 0, 0, 0]
y_pred = [1, 0, 0, 1, 0, 1, 1, 1, 0, 0]
tp = fp = fn = tn = 0
for actual, predicted in zip(y_true, y_pred):
if actual == 1 and predicted == 1:
tp += 1
elif actual == 0 and predicted == 1:
fp += 1
elif actual == 1 and predicted == 0:
fn += 1
else:
tn += 1
print(tp, fp, fn, tn) # 3 2 1 4The model found 3 of the 4 real positives (TP = 3, FN = 1). It also raised 2 false alarms among the 6 real negatives (FP = 2, TN = 4). The totals check out: 3 + 2 + 1 + 4 = 10.
Common errors
- Reading the name backwards. "False positive" does not mean the model predicted a false value. It means the model predicted positive and was wrong. Work from the second word first.
- Swapping FP and FN. Ask what the actual label was. A false positive is an actual negative. A false negative is an actual positive.
- Forgetting that the positive class is a choice. If you relabel "not fraud" as positive, every TP becomes a TN and every FP becomes a FN. The data has not changed, but the names have.
- Treating the two errors as equal. FP and FN usually cost different amounts, which later chapters use to choose metrics and thresholds.
Where this leads
Accuracy, precision, recall, specificity and F1 are all ratios of these four counts. Once you can assign every example to a cell without hesitation, those formulas become simple to read.
Exercise
A spam filter treats "spam" as the positive class. An email that is actually spam is delivered to the inbox as normal mail. Which outcome is this, and is it an FP or FN type of error?
Show answer
The actual label is positive (spam) and the predicted label is negative (not spam), so it is a false negative. The model missed a real positive.
Exercise
In a test set of 50 examples, a classifier produces TP = 8, FP = 5 and TN = 32. How many FN are there, and how many actual positives does the set contain?
Show answer
The four counts sum to 50, so FN = 50 - 8 - 5 - 32 = 5. Actual positives are TP + FN = 8 + 5 = 13.