Sub topic 6
Recall: How Many Real Positives Were Found
This chapter assumes you can read a binary confusion matrix and name its four cells (TP, FP, FN, TN). It also assumes you are comfortable with ratios and short Python snippets. Here you will learn what recall measures and how to compute it by hand and with scikit-learn. You will also learn how it behaves on imbalanced data, across thresholds and across classes, and where the common mistakes are.
What recall asks
Recall answers one question: of all the cases that really were positive, how many did the model find?
The formula uses only the actual-positive row of the matrix:
recall = TP / (TP + FN)
TP + FN is the total number of actual positives. TP is the part the model caught, and FN is the part it missed. Recall is the caught fraction.
Recall goes by other names. Sensitivity and true positive rate (TPR) mean exactly the same thing. Medicine and statistics use "sensitivity", information retrieval uses "recall", and ROC analysis uses "TPR". Treat them as one number.
False positives and true negatives never appear in the formula. A model can raise or lower its false alarms without changing recall at all. This is why recall must be read together with precision, which covers the other side.
Computing recall from the matrix
Use the same ten-example data as earlier chapters. Rows of the scikit-learn matrix are actual labels and columns are predicted labels.
from sklearn.metrics import confusion_matrix, recall_score
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()
recall_by_hand = tp / (tp + fn) # 3 / 4
print(recall_by_hand) # 0.75
print(recall_score(y_true, y_pred)) # 0.75The bottom row is [1 3]. There are four actual positives. The model found three and missed one, so recall is 3/4 = 0.75.
To find the right row by eye, locate the row for the class you care about and divide the diagonal cell by the row sum. Recall is always a row-wise quantity when rows are actual labels. If you are using a tool with the axes flipped, you divide down a column instead. Check the axes first (see the chapter on axis conventions).
Recall in plain terms
Recall tells you the cost of misses. Read it as "if a real positive shows up, what is the chance the model flags it?" A recall of 0.75 means that on average one real positive in four slips through.
Recall matters most when a missed positive is expensive:
- Disease screening, where a missed case goes untreated.
- Fraud detection, where a missed fraud is a loss.
- Safety monitoring, where a missed fault can cause damage.
In these settings a false alarm is usually cheaper than a miss, so teams often accept lower precision to get higher recall.
Recall on imbalanced data
Recall is the metric that exposes the accuracy trap. Take a dataset with 95 negatives and 5 positives, and a model that always predicts negative.
from sklearn.metrics import confusion_matrix, accuracy_score, recall_score
y_true = [0] * 95 + [1] * 5
y_pred = [0] * 100 # a model that always predicts the negative class
print(confusion_matrix(y_true, y_pred, labels=[0, 1]))
# [[95 0]
# [ 5 0]]
print(accuracy_score(y_true, y_pred)) # 0.95
print(recall_score(y_true, y_pred)) # 0.0Accuracy is 0.95, but recall is 0.0 because TP = 0 and FN = 5. The model never finds a single positive. Recall is not diluted by the many true negatives, because they are not in its formula. That makes it a good first check whenever the positive class is rare.
A model can also have recall of 1.0 and still be useless. If it predicts positive for every example, then FN = 0 and recall is 1.0, while precision collapses to the base rate of positives. A high recall alone proves nothing, so always report it next to precision.
Recall changes with the decision threshold
Most classifiers output a score or probability, and a threshold turns it into a label. Lowering the threshold flags more examples as positive. That can only turn false negatives into true positives (and true negatives into false positives), so recall never decreases as the threshold goes down.
from sklearn.metrics import confusion_matrix, recall_score
y_true = [1, 0, 1, 1, 0, 0, 1, 0, 0, 0]
scores = [0.9, 0.2, 0.4, 0.8, 0.1, 0.7, 0.6, 0.55, 0.3, 0.35]
for threshold in (0.35, 0.5, 0.65):
y_pred = [1 if s >= threshold else 0 for s in scores]
print(threshold, recall_score(y_true, y_pred))
print(confusion_matrix(y_true, y_pred, labels=[0, 1]))
# 0.35 1.0
# [[3 3]
# [0 4]]
# 0.5 0.75
# [[4 2]
# [1 3]]
# 0.65 0.5
# [[5 1]
# [2 2]]| Threshold | TP | FN | FP | Recall |
|---|---|---|---|---|
| 0.35 | 4 | 0 | 3 | 1.00 |
| 0.50 | 3 | 1 | 2 | 0.75 |
| 0.65 | 2 | 2 | 1 | 0.50 |
At 0.35 every positive is found, but three negatives are wrongly flagged. At 0.65 only one negative is wrongly flagged, but half the positives are lost. Picking a threshold means choosing where on this trade-off to sit, and recall is the number that tracks one side of it.
Recall in a multiclass matrix
For more than two classes, recall is computed per class. For a class, the "positives" are the examples whose actual label is that class. Its recall is the diagonal cell divided by its row sum.
from sklearn.metrics import confusion_matrix, recall_score
labels = ["bird", "cat", "dog"]
y_true = ["cat", "cat", "cat", "dog", "dog", "dog", "bird", "bird", "bird", "bird"]
y_pred = ["cat", "cat", "dog", "dog", "dog", "dog", "bird", "cat", "bird", "bird"]
cm = confusion_matrix(y_true, y_pred, labels=labels)
print(cm)
# [[3 1 0]
# [0 2 1]
# [0 0 3]]
# Row normalization: each row sums to 1, so the diagonal is per-class recall.
print(confusion_matrix(y_true, y_pred, labels=labels, normalize="true"))
print(recall_score(y_true, y_pred, labels=labels, average=None))
# [0.75 0.66666667 1. ]Read it row by row:
- Bird: 3 of 4 found, recall 0.75. The one miss was labelled cat.
- Cat: 2 of 3 found, recall about 0.667. The one miss was labelled dog.
- Dog: 3 of 3 found, recall 1.0.
The off-diagonal cells in a row show where the missed examples went. That tells you which class the model confuses a class with, which a single recall number cannot do. Note that the average=None call returns one value per class, in the order of labels. Combining these into one number is covered in the chapter on macro, micro and weighted averages.
Common errors
- Dividing by the wrong total. Recall divides by the row sum (actual positives). Dividing by the column sum gives precision. If your matrix has predicted labels on the rows, the two swap.
- Reading the wrong class as positive.
recall_scoretreats label 1 as positive by default. For string labels or when the rare class is 0, passpos_label. Otherwise you may be measuring the recall of the wrong class. - Reporting recall alone. Predicting everything as positive gives perfect recall. Always pair it with precision or a false positive rate.
- No positives in the data. If TP + FN = 0, recall is 0/0. scikit-learn warns and returns 0.0 by default. Use
zero_divisionto choose the value explicitly, and treat the result as "undefined" rather than "bad". - Comparing recall across datasets with different thresholds. A recall of 0.9 at a threshold of 0.2 is not comparable to a recall of 0.8 at 0.5. Compare at the same threshold or at the same false positive rate.
Summary of when to use it
| Situation | What to look at |
|---|---|
| Missing a positive is costly | Recall first, then check precision stays acceptable |
| Positives are rare | Recall, not accuracy |
| You need to see where misses go | Row-normalized multiclass matrix |
| You tune a threshold | Recall alongside precision at each threshold |
Exercise
A matrix has rows as actual labels and columns as predicted labels, with cells [[50, 10], [20, 40]] for classes [0, 1] where 1 is positive. What are the recall and the number of false negatives?
Show answer
The positive row is [20, 40], so FN = 20 and TP = 40. Recall = 40 / (40 + 20) = 40 / 60, about 0.667.
Exercise
A fraud model flags every transaction as fraud. What is its recall, and why is that figure not a sign of a good model?
Show answer
Every actual fraud is flagged, so FN = 0 and recall = 1.0. The model also flags every legitimate transaction, so false positives are huge and precision falls to the fraud base rate. Recall ignores false positives, so it cannot detect this failure on its own.
Exercise
You lower a classifier's threshold from 0.5 to 0.3. What can happen to recall, and what can you not predict about it?
Show answer
Recall stays the same or rises, because lowering the threshold can only turn false negatives into true positives. Precision can go up or down, so you cannot predict it without recomputing the matrix.