Sub topic 2
The confusion matrix: TP, FP, TN, FN
This chapter assumes you know basic Python, understand supervised binary classification with labeled data, and know what a train/test split is. You will build a confusion matrix by hand, read each cell, and check your counts against scikit-learn. Later chapters turn these counts into accuracy, precision, and recall.
From predictions to four counts
A binary classifier outputs a predicted label for each example. The test set also holds the true label for each example. Comparing the two for one example gives exactly one of four outcomes.
By convention, class 1 is the positive class. This is the class you are trying to find, such as fraud, disease, or spam. Class 0 is the negative class.
| Outcome | True label | Predicted label | Meaning |
|---|---|---|---|
| True positive (TP) | 1 | 1 | A real positive, correctly flagged |
| False positive (FP) | 0 | 1 | A negative, wrongly flagged as positive |
| True negative (TN) | 0 | 0 | A real negative, correctly left alone |
| False negative (FN) | 1 | 0 | A real positive that the model missed |
Read each name in two parts. The second word is what the model predicted (positive or negative). The first word says whether that prediction was right (true or false). So a false positive is a wrong positive prediction.
The confusion matrix is a 2x2 table that holds these four counts. Every example lands in exactly one cell, so the four counts add up to the size of the test set.
Counting the cells by hand
Here are ten examples. Four are truly positive and six are truly negative.
y_true = [1, 0, 1, 1, 0, 0, 0, 0, 0, 1]
y_pred = [1, 0, 0, 1, 0, 1, 0, 0, 0, 1]
tp = sum(t == 1 and p == 1 for t, p in zip(y_true, y_pred))
fp = sum(t == 0 and p == 1 for t, p in zip(y_true, y_pred))
tn = sum(t == 0 and p == 0 for t, p in zip(y_true, y_pred))
fn = sum(t == 1 and p == 0 for t, p in zip(y_true, y_pred))
print(tp, fp, tn, fn) # 3 1 5 1Each line counts one condition. Python treats True as 1 and False as 0, so sum over the booleans returns the number of matches. The result is TP=3, FP=1, TN=5, FN=1.
Check the totals before you go on:
- Real positives are TP + FN = 3 + 1 = 4, which matches the four 1s in
y_true. - Real negatives are TN + FP = 5 + 1 = 6, which matches the six 0s in
y_true. - All cells sum to 3 + 1 + 5 + 1 = 10, the number of examples.
If these sums do not match your data, a condition in your counting code is wrong.
Reading the matrix as a table
The same counts are usually drawn as a grid. Rows are the true class and columns are the predicted class.
| Row | Predicted 0 | Predicted 1 |
|---|---|---|
| **True 0** | TN = 5 | FP = 1 |
| **True 1** | FN = 1 | TP = 3 |
The correct predictions sit on the diagonal from top left to bottom right. The errors sit off the diagonal. The two errors are different kinds of mistake. A false positive is a false alarm. A false negative is a miss. Later chapters show that these two errors often have very different costs.
Checking against scikit-learn
sklearn.metrics.confusion_matrix returns the same grid as a NumPy array.
from sklearn.metrics import confusion_matrix
y_true = [1, 0, 1, 1, 0, 0, 0, 0, 0, 1]
y_pred = [1, 0, 0, 1, 0, 1, 0, 0, 0, 1]
cm = confusion_matrix(y_true, y_pred)
print(cm) # [[5 1]
# [1 3]]
tn, fp, fn, tp = cm.ravel()
print(tp, fp, tn, fn) # 3 1 5 1Common errors
- Swapping the arguments.
confusion_matrix(y_pred, y_true)transposes the result, which swaps FP and FN without raising an error. The order is always true labels first, then predictions. - Treating the wrong class as positive. The names TP and FP depend on which class you call positive. If you flip the meaning of 0 and 1, TP and TN swap, and FP and FN swap.
- Counting on training data. Build the matrix from a held-out test set. A matrix from training data shows how well the model memorized, not how well it generalizes.
Exercise
A classifier is tested on 8 examples. ytrue = [1, 1, 0, 0, 0, 1, 0, 0] and ypred = [1, 0, 0, 1, 0, 1, 0, 0]. Find TP, FP, TN, and FN, then confirm that they add up to the test set size.
Show answer
Compare pairs in order: (1,1) TP, (1,0) FN, (0,0) TN, (0,1) FP, (0,0) TN, (1,1) TP, (0,0) TN, (0,0) TN. So TP=2, FP=1, TN=4, FN=1. The sum is 2 + 1 + 4 + 1 = 8, which equals the number of examples. As a second check, TP + FN = 3 matches the three 1s in y_true.
Exercise
In scikit-learn's confusion_matrix output for labels 0 and 1, which cell holds the false negatives?
Show answer
Row 1, column 0, because rows are true classes and columns are predicted classes. That cell counts examples whose true class is 1 and whose predicted class is 0.