Sub topic 9
Specificity and false positive rate
This chapter assumes you can already find TP, FP, FN and TN in a binary confusion matrix. It also assumes you know recall and can read short Python snippets. Recall measures how well a model finds the positives. Specificity measures the same thing for the negatives.
Specificity: recall for the negative class
Specificity is the share of actual negatives that the model correctly labels negative. It is also called the true negative rate.
specificity = TN / (TN + FP)The denominator is the whole actual-negative row of the matrix. Recall uses the actual-positive row.
False positive rate: the complement
The false positive rate (FPR) is the share of actual negatives that the model wrongly flags as positive. It is also called the fall-out.
FPR = FP / (FP + TN) = 1 - specificityThe two numbers always add to 1. Specificity tells you how often a negative case is left alone. FPR tells you how often it triggers a false alarm. Reports and ROC curves usually use FPR.
Computing both from the matrix
This uses the same labels as the earlier chapters.
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]
tn, fp, fn, tp = confusion_matrix(y_true, y_pred, labels=[0, 1]).ravel()
specificity = tn / (tn + fp)
fpr = fp / (fp + tn)
recall = tp / (tp + fn)
print(round(specificity, 3)) # 0.667
print(round(fpr, 3)) # 0.333
print(round(recall, 3)) # 0.75There are six actual negatives. The model gets four right and raises false alarms on two, so specificity is 4/6 and FPR is 2/6.
Why precision is not enough
Precision and FPR both involve false positives, but they divide by different things. Precision divides by the number of predicted positives, so it changes when the class mix changes. FPR divides by the actual negatives, so it describes the model's behavior on negative cases whatever the class mix. That makes FPR a steadier measure when positives are rare.
A model can have a very low FPR and still have poor precision. Suppose a disease affects 1 in 1,000 people and the FPR is 1%. Out of 100,000 people, about 100 are sick and 99,900 are healthy. The model flags about 999 healthy people, and that overwhelms the true positives it finds.
Specificity and the decision threshold
Recall and FPR move together as you change the threshold. Using the scores from the threshold chapter:
| Threshold | TN | FP | FN | TP | Recall | FPR |
|---|---|---|---|---|---|---|
| 0.35 | 3 | 3 | 0 | 4 | 1.00 | 0.50 |
| 0.50 | 4 | 2 | 1 | 3 | 0.75 | 0.33 |
| 0.65 | 5 | 1 | 2 | 2 | 0.50 | 0.17 |
Raising the threshold lowers FPR and raises specificity. It also lowers recall. Plotting recall against FPR across all thresholds gives the ROC curve.
When to prefer it
Specificity matters when false alarms are costly. Examples are a spam filter that hides real email, or a screening test that sends healthy people to invasive follow-ups. In those cases, report specificity or FPR next to recall so both error types are visible.
Exercise
A model is tested on 200 cases, of which 50 are actually positive. It produces 10 false positives and 5 false negatives. What are its specificity, FPR and recall?
Show answer
There are 150 actual negatives, so TN = 150 - 10 = 140. Specificity = 140 / 150 ≈ 0.933 and FPR = 10 / 150 ≈ 0.067. TP = 50 - 5 = 45, so recall = 45 / 50 = 0.90.