Sub topic 6
Recall: how many real positives were found
This chapter assumes you can read basic Python and know what a supervised classifier is. It also assumes you know what a train/test split is and have seen the confusion matrix from earlier in this tutorial. Recall is computed from two of its four cells, so a quick review helps.
What recall measures
Recall answers one question: of all the examples that really are positive, how many did the model find?
A positive is the class you are looking for, such as fraud, disease, or spam. The model either flags a real positive (a true positive, TP) or misses it (a false negative, FN). Recall is the share that gets flagged:
recall = TP / (TP + FN)
The denominator, TP + FN, is the number of real positives in the data. Recall uses only the positive row of the confusion matrix. False positives and true negatives do not appear in the formula.
Other names for the same number are sensitivity (common in medicine) and true positive rate, or TPR (common in ROC analysis). If you see one of these names in a paper or library, it is the same quantity.
Computing recall by hand
Use the same ten labels as in the confusion matrix chapter. There are four real positives, and the model flags three of them.
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.75Recall is 3 / (3 + 1) = 0.75. The model found 75% of the real positives and missed 25%.
The false positive at index 5 does not change this number. If the model had made ten more false positives, recall would still be 0.75. That property matters for the next two sections.
Why recall exists: the accuracy paradox
Accuracy counts every correct prediction, so a majority class can hide failure on the minority class. Recall exposes that failure directly. Take a data set with one fraud case in 100 transactions and a model that always predicts "not fraud".
# 1 fraud case in 100 transactions; the model predicts "not fraud" every time
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. Recall is 0.0, because the model found none of the real positives. The model is useless for its purpose, and recall is the metric that shows it.
Recall alone can be gamed
A model that never predicts the positive class gets recall 0. The opposite extreme is a model that predicts positive for everything. It never misses a positive, so its recall is perfect.
y_true = [1] + [0] * 99
y_pred = [1] * 100 # flag every transaction as fraud
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))
fp = sum(t == 0 and p == 1 for t, p in zip(y_true, y_pred))
print(tp / (tp + fn)) # 1.0 recall
print(tp / (tp + fp)) # 0.01 precision
print(sum(t == p for t, p in zip(y_true, y_pred)) / len(y_true)) # 0.01 accuracyRecall is 1.0 while precision and accuracy are both 0.01. A high recall figure on its own says nothing about how many false alarms came with it. Always report recall next to precision. The two metrics cover each other's blind spots, and the F1 chapter combines them into one number.
What a false negative costs
Recall is the metric to prioritize when a missed positive is more expensive than a false alarm. Typical cases:
- Disease screening. A missed case can go untreated. A false alarm usually costs a follow-up test.
- Fraud detection. A missed fraud is a direct financial loss. A false alarm costs an analyst's review time.
- Safety monitoring. A missed defect or intrusion can cause damage that a false alarm would not.
In these settings, teams often set a minimum recall target, such as 0.95, and then try to get the best precision they can at that level. Choosing between precision and recall on cost grounds is covered fully in the chapter on error costs. Here the point is that recall is the number you watch when misses hurt.
Recall and the decision threshold
Many classifiers output a score, such as a probability, and then apply a threshold. Any example with a score at or above the threshold is predicted positive. Lowering the threshold flags more examples. That can only turn false negatives into true positives, or leave them unchanged, so recall never decreases as the threshold goes down.
Using the scores from the threshold chapter:
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.8, 0.5, 0.3):
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.8 0.33
# 0.5 0.67
# 0.3 1.0Recall rises from 0.33 to 1.0 as the threshold drops. Precision moves the other way, because the lower threshold also admits more false positives. This is the precision-recall trade-off.
The trade-off is a consequence of how the model is used, not a flaw in it. If a team says "we need higher recall", changing the threshold is often the cheapest fix, and it needs no retraining.
Computing recall with scikit-learn
recall_score in sklearn.metrics computes the same value. Check it against your manual calculation.
from sklearn.metrics import confusion_matrix, recall_score
y_true = [1, 0, 1, 1, 0, 0, 0, 0, 0, 1]
y_pred = [1, 0, 0, 1, 0, 1, 0, 0, 0, 1]
print(recall_score(y_true, y_pred)) # 0.75
tn, fp, fn, tp = confusion_matrix(y_true, y_pred).ravel()
print(tp / (tp + fn)) # 0.75confusion_matrix returns rows for true classes and columns for predicted classes, ordered 0 then 1. Flattening a 2x2 matrix with ravel() gives tn, fp, fn, tp in that order. The order is easy to get wrong when you unpack it. The recall formula uses the bottom row, fn and tp.
By default recall_score treats label 1 as the positive class. If your positive label is something else, such as the string "fraud", pass pos_label="fraud". If you leave it out with string labels, scikit-learn raises an error rather than guessing.
Edge case: no real positives
Recall divides by TP + FN. If the evaluation set has no real positives, that denominator is zero and recall is undefined.
from sklearn.metrics import recall_score
y_true = [0, 0, 0, 0]
y_pred = [0, 1, 0, 0]
print(recall_score(y_true, y_pred)) # 0.0 (with a warning)
print(recall_score(y_true, y_pred, zero_division=1)) # 1.0
print(recall_score(y_true, y_pred, zero_division=0)) # 0.0The zero_division argument sets the value returned in this case. With the default "warn", you get 0.0 and a warning. A zero here does not mean the model is bad, because there was nothing to find. A one does not mean the model is good either.
This case appears more often than you might expect:
- A very small test set or a rare class can contain no positives by chance.
- A per-group or per-day evaluation can contain slices with no positives.
- A multiclass evaluation can include a class that never appears in
y_true.
Choose zero_division deliberately, and note in your report how many slices were affected. Averaging in a hidden 0.0 or 1.0 shifts the overall number without telling the reader.
Recall for more than two classes
In a multiclass problem, recall is computed for each class separately. For each class, treat it as the positive class and everything else as negative. Then the formula is the same: correct predictions of that class divided by all true examples of that class.
from sklearn.metrics import recall_score
y_true = [0, 1, 2, 2, 2]
y_pred = [0, 2, 2, 2, 1]
print(recall_score(y_true, y_pred, average=None).round(2)) # [1. 0. 0.67]Class 0 was found 1 of 1 times, class 1 was missed, and class 2 was found 2 of 3 times. With average=None you get one value per class, in sorted label order. Combining them into one number through macro, micro, or weighted averaging is covered in the multiclass chapter.
Common mistakes
| Mistake | Why it is wrong | Fix |
|---|---|---|
| Confusing recall with precision | Precision divides by predicted positives (TP + FP). Recall divides by actual positives (TP + FN). | Ask what the denominator counts: predictions or real cases. |
| Swapping the arguments to `recall_score` | The signature is `(y_true, y_pred)`. Swapped arguments compute precision instead, with no error. | Keep the true labels first. |
| Unpacking `confusion_matrix` as `tp, fp, fn, tn` | The flattened order is `tn, fp, fn, tp`. | Unpack in that order, or index the matrix explicitly. |
| Reporting recall measured on training data | A model can memorize training positives, so recall looks better than it will in use. | Compute recall on a held-out test set. |
| Treating high recall as success | Flagging everything gives recall 1.0. | Report precision or false positive counts alongside it. |
Summary
- Recall is TP / (TP + FN), the fraction of real positives the model finds. It is also called sensitivity or true positive rate.
- It reads only the positive row of the confusion matrix, so false positives never affect it.
- It exposes failures that accuracy hides on imbalanced data, as in the 99% accurate model with recall 0.0.
- Predicting everything as positive gives recall 1.0, so recall must be read together with precision.
- Lowering the decision threshold never lowers recall, but it usually costs precision.
- When there are no real positives in the evaluation data, recall is undefined, and
zero_divisiondecides what is reported.
Exercise
A model is tested on 200 emails. 40 are really spam. The model flags 50 emails as spam, and 30 of those flags are correct. Compute the recall. Then say what recall would be if the model flagged 80 emails as spam but still caught the same 30 real spam emails.
Show answer
TP = 30, and the number of real positives is 40, so FN = 40 - 30 = 10. Recall = 30 / (30 + 10) = 0.75. In the second case, the model still caught the same 30 real spam emails, so TP and FN are unchanged and recall stays 0.75. The extra 30 flags would be false positives, which lower precision but do not enter the recall formula.
Exercise
You run recall_score(y_true, y_pred) on a test set in which y_true contains no 1s, and the result is 0.0 with a warning. Does this mean the model failed to find any positives? What should you do?
Show answer
No. With no real positives there is nothing to find, so TP + FN is 0 and recall is undefined. scikit-learn returns the zero_division value, 0.0 by default with a warning. Check the label counts in the test set, and consider a larger or stratified split so positives are present. If you must report a number, set zero_division explicitly and say in the report that recall was undefined for this data.