Sub topic 9
Specificity and false positive rate
This chapter assumes you can already read a binary confusion matrix and name its four cells (TP, FP, FN, TN). It also assumes you know that scikit-learn puts actual labels in rows and predicted labels in columns. You should be comfortable with recall and precision as ratios of those cells.
The negative row of the matrix
Recall looks only at the actual positives. It asks what share of them the model found. Specificity does the same for the actual negatives. It asks what share of them the model correctly left alone.
The actual-negative row of the matrix has two cells:
- TN (true negatives): actual negatives predicted negative.
- FP (false positives): actual negatives predicted positive.
Specificity is the share of that row that lands in TN:
specificity = TN / (TN + FP)The false positive rate (FPR) is the share that lands in FP:
FPR = FP / (TN + FP)Both ratios have the same denominator, the total number of actual negatives. So they always sum to 1:
FPR = 1 - specificitySpecificity is also called the true negative rate (TNR). FPR is sometimes called the fall-out. They carry the same information. Specificity reads "higher is better", and FPR reads "lower is better".
Mirror image of recall
Recall and specificity are mirror images. Each is computed within one actual class, so each uses one row of the matrix.
| Rate | Row used | Formula | Also called |
|---|---|---|---|
| Recall | Actual positives | TP / (TP + FN) | Sensitivity, true positive rate (TPR) |
| Miss rate | Actual positives | FN / (TP + FN) | False negative rate (FNR) |
| Specificity | Actual negatives | TN / (TN + FP) | True negative rate (TNR) |
| FPR | Actual negatives | FP / (TN + FP) | Fall-out |
Precision is different. Its denominator is TP + FP, which is a column of the matrix: everything the model predicted positive. That difference matters later in this chapter.
Computing it from the example matrix
Here is the matrix from earlier in the tutorial. It has 4 TN, 2 FP, 1 FN and 3 TP.
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]
tn, fp, fn, tp = confusion_matrix(y_true, y_pred, labels=[0, 1]).ravel()
specificity = tn / (tn + fp) # 4 / 6, about 0.667
fpr = fp / (tn + fp) # 2 / 6, about 0.333
recall = tp / (tp + fn) # 3 / 4 = 0.75
print(round(specificity, 3), round(fpr, 3), round(recall, 3))
# 0.667 0.333 0.75
# scikit-learn has no specificity function, but recall of the
# negative class is the same quantity.
print(round(recall_score(y_true, y_pred, pos_label=0), 3)) # 0.667The last line works because recall is "found items of this class divided by all items of this class". If you declare class 0 as the one you care about, recall becomes specificity.
The model wrongly flags a third of the actual negatives (2 of 6), while it finds three quarters of the actual positives.
Where specificity matters
Specificity matters when a false alarm has a real cost:
- Spam filtering. A false positive sends a real email to the spam folder. Users tolerate some missed spam, but they do not tolerate lost mail.
- Fraud blocking. A false positive declines a legitimate purchase and annoys a customer.
- Medical screening. A false positive can lead to anxiety, follow-up tests, and sometimes unnecessary treatment.
High specificity means few false alarms relative to the number of real negatives. This is a statement about a rate. It does not tell you how many of the flagged cases are real. For that you need precision.
Specificity does not depend on class balance
Recall and specificity are each computed inside a single actual class. If you collect twice as many negatives from the same population, the rates stay about the same, because the denominator grows along with the counts. Precision does not behave this way, because it mixes the two rows.
Consider a screening test with 90% sensitivity and 95% specificity, applied to 1,000 people where 1% have the condition.
| Quantity | Calculation | Result |
|---|---|---|
| Actual positives | 1,000 × 0.01 | 10 |
| Actual negatives | 1,000 − 10 | 990 |
| TP | 10 × 0.90 | 9 |
| FP | 990 × 0.05 | 49.5 (about 50) |
| Precision | 9 / (9 + 49.5) | about 0.15 |
The test looks strong on both rates, yet only about 15% of positive results are real. A low FPR can still produce many more false alarms than true hits when negatives vastly outnumber positives.
This is the same imbalance issue seen in the accuracy chapter, from the opposite side. Accuracy hides a poor recall. A good specificity can hide a poor precision.
Specificity moves with the threshold
Most classifiers output a score, and the decision threshold turns that score into a label. The next chapter covers the threshold in detail. The point here is that raising it removes false positives, so specificity rises, while recall falls.
from sklearn.metrics import confusion_matrix
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]
tn, fp, fn, tp = confusion_matrix(y_true, y_pred, labels=[0, 1]).ravel()
print(
f"threshold={threshold} "
f"specificity={tn / (tn + fp):.3f} "
f"FPR={fp / (tn + fp):.3f} "
f"recall={tp / (tp + fn):.3f}"
)
# threshold=0.35 specificity=0.500 FPR=0.500 recall=1.000
# threshold=0.5 specificity=0.667 FPR=0.333 recall=0.750
# threshold=0.65 specificity=0.833 FPR=0.167 recall=0.500As the threshold climbs, specificity goes from 0.500 to 0.833 and recall drops from 1.000 to 0.500. You can raise one rate only by accepting a loss in the other, unless you improve the model itself.
Plotting recall (TPR) against FPR for every possible threshold gives the ROC curve. In scikit-learn, roc_curve(y_true, scores) returns the FPR values, the TPR values, and the thresholds that produced them. The x-axis of a ROC plot is the quantity from this chapter. A point at the far left means few false alarms.
Specificity in a multiclass matrix
With more than two classes, there is no single "negative" class. You compute specificity one class at a time, one-vs-rest. For class c, treat c as positive and everything else as negative. Then:
- TP is the diagonal cell for
c. - FP is the column sum for
cminus TP. These are other classes predicted asc. - FN is the row sum for
cminus TP. These arecitems predicted as something else. - TN is everything left over: the total minus TP, FP and FN.
import numpy as np
from sklearn.metrics import confusion_matrix
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)
tp = np.diag(cm)
fp = cm.sum(axis=0) - tp
fn = cm.sum(axis=1) - tp
tn = cm.sum() - tp - fp - fn
specificity = tn / (tn + fp)
for name, value in zip(labels, specificity):
print(name, round(float(value), 3))
# bird 1.0
# cat 0.857
# dog 0.857Bird has no false positives. No cat or dog was ever labeled as a bird, so its specificity is 1.0. The cat class has one false positive, a bird predicted as cat, among 7 non-cat items, which gives 6/7.
Common mistakes
- Using the wrong row. Specificity divides by the actual negatives, so it uses the negative row. If your matrix has predicted labels in rows, as some tools do, your TN and FP cells sit in different places. Check the axes first, as you did earlier in the tutorial.
- Mixing up FPR and the false discovery rate. FPR is FP divided by actual negatives. The false discovery rate is FP divided by predicted positives, which equals 1 − precision. They use different denominators and answer different questions.
- Treating specificity as the opposite of recall. The two are independent measurements on different rows. A model can score high on both, or low on both.
- Dividing by zero. If the data contains no actual negatives, then TN + FP = 0 and specificity is undefined. Handle this case explicitly instead of letting NaN values spread into reports.
Exercise
A fraud model is tested on 2,000 legitimate transactions and 40 fraudulent ones. It flags 100 of the legitimate transactions and catches 30 of the fraudulent ones. Compute the specificity, the FPR, the recall, and the precision.
Show answer
Actual negatives are the 2,000 legitimate transactions. FP = 100, so TN = 1,900. Specificity = 1,900 / 2,000 = 0.95 and FPR = 100 / 2,000 = 0.05. TP = 30 and FN = 10, so recall = 30 / 40 = 0.75. Precision = 30 / (30 + 100) = 30 / 130, about 0.23. The specificity looks excellent, yet fewer than a quarter of flagged transactions are fraud.
Exercise
A threshold change moves a model from TN=80, FP=20 to TN=90, FP=10. What happens to specificity and FPR, and what do you expect to happen to recall?
Show answer
Specificity rises from 80/100 = 0.80 to 90/100 = 0.90, and FPR falls from 0.20 to 0.10. This pattern is consistent with a higher threshold, so you should expect recall to fall or stay the same, because positives that scored just below the new threshold become false negatives. The total of actual negatives stays 100, which is why the two rates still sum to 1.