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 cells of a binary confusion matrix and how to assign each prediction to one of them. Matrix layout, axis conventions and the metrics built on these counts come in later chapters.
Why one prediction has four possible fates
A binary classifier answers a yes/no question for each example, such as "Is this email spam?" or "Does this scan show a tumor?" Each example has an actual label (the ground truth) and a predicted label (what the model said). Because each label has two values, there are exactly four combinations.
By convention, one class is called the positive class and the other the negative class. The positive class is the one you are trying to detect. It is not the "good" class. In fraud detection, "fraud" is positive. In a medical test, "disease present" is positive.
The four outcomes are:
| Outcome | Actual | Predicted | Plain meaning |
|---|---|---|---|
| True positive (TP) | positive | positive | A real positive, correctly flagged |
| False positive (FP) | negative | positive | A false alarm |
| False negative (FN) | positive | negative | A missed positive |
| True negative (TN) | negative | negative | A real negative, correctly left alone |
How to read the names
Each name has two words, and each word answers a different question.
- The second word (positive or negative) is what the model predicted.
- The first word (true or false) says whether that prediction was correct.
So "false negative" means the model predicted negative and was wrong, which means the example was actually positive. When you hesitate over a name, work through those two steps: what did the model say, then was it right?
The same pairs appear in statistics textbooks under other names:
- A false positive is a Type I error.
- A false negative is a Type II error.
Many ML teams prefer "false positive" and "false negative" because they are harder to mix up.
A worked example: counting by hand
Take ten emails, where 1 means spam (the positive class) and 0 means not spam.
y_true = [1, 0, 1, 1, 0, 0, 1, 0, 0, 0]
y_pred = [1, 0, 0, 1, 0, 1, 1, 1, 0, 0]
counts = {"TP": 0, "FP": 0, "FN": 0, "TN": 0}
for actual, predicted in zip(y_true, y_pred):
if actual == 1 and predicted == 1:
counts["TP"] += 1
elif actual == 0 and predicted == 1:
counts["FP"] += 1
elif actual == 1 and predicted == 0:
counts["FN"] += 1
else:
counts["TN"] += 1
print(counts) # {'TP': 3, 'FP': 2, 'FN': 1, 'TN': 4}Walk through it example by example:
- Email 1 is spam and was flagged: TP.
- Email 3 is spam but was let through: FN.
- Email 6 is not spam but was flagged: FP.
- Email 8 is also not spam but was flagged: FP.
- Emails 2, 5, 9 and 10 are not spam and were left alone: TN.
- Emails 1, 4 and 7 are the three TPs.
Two checks are worth building into your habits.
- The four counts always sum to the number of examples. Here 3 + 2 + 1 + 4 = 10.
- The actual positives are TP + FN (here 4). The actual negatives are FP + TN (here 6). Those totals depend only on the true labels, so they stay fixed no matter how the model behaves.
Likewise, the predicted positives are TP + FP (here 5) and the predicted negatives are FN + TN (here 5). These totals change when the model changes.
Getting the same counts from scikit-learn
In practice you rarely write the loop. confusion_matrix returns a 2×2 array, and ravel() flattens it. For binary labels, the flattened order is TN, FP, FN, TP.
from sklearn.metrics import confusion_matrix
y_true = [1, 0, 1, 1, 0, 0, 1, 0, 0, 0]
y_pred = [1, 0, 0, 1, 0, 1, 1, 1, 0, 0]
cm = confusion_matrix(y_true, y_pred, labels=[0, 1])
print(cm)
# [[4 2]
# [1 3]]
tn, fp, fn, tp = cm.ravel()
print(tn, fp, fn, tp) # 4 2 1 3Notice that the order is not TP, FP, FN, TN. Writing tp, fp, fn, tn = cm.ravel() is a common bug. It runs without error and silently gives wrong numbers. The next chapter explains why the cells fall in this order and how to check which axis holds which label.
The choice of positive class changes the names
The labels "positive" and "negative" are your decision, and the model's behavior does not depend on it. Swapping them relabels the cells. Every TP becomes a TN, every TN becomes a TP, and FP and FN trade places.
y_true = [1, 0, 1, 1, 0, 0, 1, 0, 0, 0]
y_pred = [1, 0, 0, 1, 0, 1, 1, 1, 0, 0]
def outcomes(y_true, y_pred, positive):
pairs = list(zip(y_true, y_pred))
tp = sum(1 for a, p in pairs if a == positive and p == positive)
fp = sum(1 for a, p in pairs if a != positive and p == positive)
fn = sum(1 for a, p in pairs if a == positive and p != positive)
tn = sum(1 for a, p in pairs if a != positive and p != positive)
return {"TP": tp, "FP": fp, "FN": fn, "TN": tn}
print(outcomes(y_true, y_pred, positive=1)) # {'TP': 3, 'FP': 2, 'FN': 1, 'TN': 4}
print(outcomes(y_true, y_pred, positive=0)) # {'TP': 4, 'FP': 1, 'FN': 2, 'TN': 3}This matters because metrics built on TP, FP and FN, such as precision and recall, change when you swap the positive class. Always state which class you treated as positive when you report results. When the question is "find the rare thing," the rare thing is almost always the positive class.
The two errors are not equally costly
TP and TN are correct predictions. FP and FN are the two different ways to be wrong, and they usually have different consequences.
| Scenario | False positive costs | False negative costs |
|---|---|---|
| Spam filter | A real email lands in the spam folder | A spam email reaches the inbox |
| Cancer screening | An unnecessary follow-up test and worry | A missed diagnosis |
| Card fraud alert | A blocked legitimate purchase | Money stolen |
| Loan approval (positive = "will default") | A good applicant is rejected | A bad loan is issued |
Whether a false positive or a false negative is worse depends on the application. A single "number of mistakes" figure hides that difference, which is why the confusion matrix keeps the two error types in separate cells.
Edge cases to watch for
Only one class appears in the data
If both y_true and y_pred contain only one class and you do not pass labels, scikit-learn infers the labels from the data and returns a 1×1 matrix. Unpacking four values then fails.
from sklearn.metrics import confusion_matrix
y_true = [0, 0, 0]
y_pred = [0, 0, 0]
print(confusion_matrix(y_true, y_pred))
# [[3]]
print(confusion_matrix(y_true, y_pred, labels=[0, 1]))
# [[3 0]
# [0 0]]Passing labels=[0, 1] fixes the shape at 2×2 and fixes which label comes first. This is a good default for every binary call. Small evaluation batches and per-group slices of a dataset often contain a single class, so it matters in practice.
Zero in a cell
A zero is not an error. A model that never predicts positive has TP = 0 and FP = 0. A model that never misses a positive has FN = 0. Zeros in the FP or FN cells are exactly what you hope to see, but you should check what they cost in the other cells. Later chapters show how a "perfect" FN column can come from a model that flags everything.
Predicted labels from scores
Many models output a score or probability, not a label. The predicted label comes from comparing the score to a threshold, and the cell counts depend on that threshold. A matrix is therefore a snapshot at one threshold, not a property of the model alone. A later chapter covers this in detail.
Counts versus rates
The four cells are counts, so they depend on the size of the dataset. 40 false positives means something different in 100 examples than in 1,000,000. Ratios built from the counts, which later chapters introduce, make results comparable across datasets.
Summary of the working model
- Choose the positive class and write it down.
- For each example, ask what the model predicted, then whether it was right.
- Tally the four counts and check that they sum to the number of examples.
- Check the row totals against the true class sizes.
- Fix
labelswhen calling a library, and unpack the cells in the order the library documents.
Exercise
A model screens 200 patients for a disease. Of the 40 who have the disease, it flags 34. Of the 160 who are healthy, it flags 12. Treating "disease" as positive, find TP, FP, FN and TN. What happens to each count if you treat "healthy" as positive instead?
Show answer
With "disease" as positive: TP = 34 (sick and flagged), FN = 40 - 34 = 6 (sick but not flagged), FP = 12 (healthy but flagged), TN = 160 - 12 = 148 (healthy and not flagged). The sum is 34 + 12 + 6 + 148 = 200. With "healthy" as positive, TP and TN swap and FP and FN swap: TP = 148, TN = 34, FP = 6, FN = 12.
Exercise
A colleague runs tp, fp, fn, tn = confusion_matrix(y_true, y_pred).ravel() on a binary problem with labels 0 and 1, and gets tp = 4, fp = 2, fn = 1, tn = 3. In this data the actual positives number 4. Which assignment is wrong, and what are the correct values?
Show answer
The unpacking order is wrong. The array flattens in the order TN, FP, FN, TP, so the values are tn = 4, fp = 2, fn = 1, tp = 3. This is consistent with 4 actual positives: TP + FN = 3 + 1 = 4. The colleague's version gives TP + FN = 4 + 1 = 5, which contradicts the known positive count.