Sub topic 6
Recall: how many real positives were found
This chapter assumes you know basic Python, supervised classification with labeled data, train and test splits, and the confusion matrix counts TP, FP, TN, and FN from the earlier chapters.
What recall measures
Recall answers one question: of all the cases that are truly positive, how many did the model find? It is also called sensitivity or the true positive rate.
The formula uses the first column of the confusion matrix, the true positives and the false negatives:
recall = TP / (TP + FN)
The denominator TP + FN is the number of real positives in the data. A false negative is a real positive that the model missed. Every miss lowers recall.
A worked example
Use the same ten labels and predictions as in the confusion matrix chapter.
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))
fn = sum(t == 1 and p == 0 for t, p in zip(y_true, y_pred))
recall = tp / (tp + fn)
print(tp, fn, recall) # 3 1 0.75There are four real positives. The model found three and missed one, so recall is 3 / 4 = 0.75.
Notice what recall ignores. The false positive at index 5 does not appear in the formula. A model could flag every single case as positive and reach a recall of 1.0, because it would miss nothing. Recall alone cannot tell you whether the positive predictions are trustworthy. That is the job of precision, so the two metrics are read together.
Recall on imbalanced data
Accuracy hides the failure of a model that never predicts the positive class. Recall exposes it. In the fraud example, one transaction in 100 is fraud and the model always predicts "not fraud".
y_true = [1] + [0] * 99
y_pred = [0] * 100
correct = sum(t == p for t, p in zip(y_true, y_pred))
print(correct / len(y_true)) # 0.99
tp = 0
fn = 1
print(tp / (tp + fn)) # 0.0Accuracy is 0.99, yet recall is 0.0 because the model found none of the fraud. When the rare class is the one you care about, recall is a better first check than accuracy.
When to favor recall
Favor recall when a false negative costs more than a false positive. In these cases, missing a real positive is the expensive mistake:
- Screening for a serious disease. A missed case may go untreated, while a false alarm leads to a follow-up test.
- Detecting fraud or security intrusions. A missed event can cause direct losses.
- Finding safety defects in manufactured parts.
The price is more false positives. Someone has to review those extra alerts, so a recall-focused system needs a review process that can handle them.
Recall and the decision threshold
Many models output a score, and a threshold turns the score into a class. Lowering the threshold labels more cases as positive. That catches more real positives, so recall rises or stays the same. It also admits more false positives, so precision usually falls.
y_true = [1, 1, 1, 0, 0, 0, 0, 0]
scores = [0.9, 0.6, 0.4, 0.7, 0.3, 0.2, 0.1, 0.05]
for threshold in (0.3, 0.5, 0.8):
y_pred = [int(s >= threshold) for s in scores]
tp = sum(t == 1 and p == 1 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(threshold, round(tp / (tp + fn), 2))
# 0.3 1.0
# 0.5 0.67
# 0.8 0.33At a threshold of 0.3 the model catches all three positives. At 0.8 it catches only one of them.
Common errors
- Dividing by the wrong total. Recall divides by real positives (TP + FN), not by predicted positives (TP + FP). The second ratio is precision.
- Treating high recall as proof of quality. A model that predicts positive for everything has perfect recall. Always report precision beside it.
- Computing recall on a set with no positives. If TP + FN is zero, the formula divides by zero. scikit-learn warns and returns 0.0 by default for this case.
- Reading recall from training data. Compute it on held-out test data. Training recall is usually too optimistic.
Exercise
A model screens 200 patients. 20 patients truly have the condition. The model flags 30 patients as positive, and 16 of those really have the condition. What is the recall, and how many real cases did it miss?
Show answer
TP = 16 and real positives = 20, so FN = 20 - 16 = 4. Recall = 16 / 20 = 0.8. The model missed 4 real cases. The 14 false positives (30 - 16) do not affect recall.
Exercise
You lower a classifier's threshold from 0.5 to 0.2. What usually happens to recall and to precision, and why?
Show answer
Recall rises or stays the same, because more cases are labeled positive and fewer real positives are missed. Precision usually falls, because the extra positive predictions include more false positives.