What is specificity?
Specificity measures the proportion of actual negative cases a binary classifier correctly predicts as negative. It answers: *“Of the cases that truly do not belong to the positive class, how many did the model correctly clear?”* It is also called the true negative rate (TNR).
Formula
Specificity = TN / (TN + FP)- TN (true negatives): actual negatives correctly predicted negative.
- FP (false positives): actual negatives incorrectly predicted positive.
- TN + FP: the total number of actual negatives, not all evaluated cases.
Worked example: quality control
A manufacturing inspection system treats Defective as the positive class and Good as the negative class. Among 80 actually good products:
- 72 are correctly classified as good (TN = 72).
- 8 are incorrectly flagged as defective (FP = 8).
Specificity = 72 / (72 + 8)
= 72 / 80
= 0.90 = 90%Interpretation: the model correctly clears 9 out of every 10 good products in this sample. The remaining 10% are false alarms.
Relationship to false positive rate
The false positive rate (FPR) is the fraction of actual negatives incorrectly flagged as positive:
FPR = FP / (FP + TN)
Specificity = 1 - FPRHere, FPR = 8 / 80 = 10%, so specificity = 1 - 0.10 = 90%.
Specificity versus recall
- Recall (sensitivity/TPR): Among actual positives, what fraction did we identify?
TP / (TP + FN). - Specificity (TNR): Among actual negatives, what fraction did we identify?
TN / (TN + FP).
These two metrics examine different actual classes. A model can achieve high specificity while missing many positives, so examine both when the distinction matters.
Python example
PYTHONfrom sklearn.metrics import confusion_matrix # 1 = defective, 0 = good # TN=72, FP=8, FN=5, TP=15 y_true = [0]*72 + [0]*8 + [1]*5 + [1]*15 y_pred = [0]*72 + [1]*8 + [0]*5 + [1]*15 tn, fp, fn, tp = confusion_matrix( y_true, y_pred, labels=[0, 1] ).ravel() specificity = tn / (tn + fp) print("Specificity:", specificity) # 0.9 print("False positive rate:", fp / (tn + fp)) # 0.1
When is specificity useful?
Specificity is important when you want to avoid false alarms among actual negatives, such as unnecessarily flagging good products or legitimate transactions. It is also one ingredient of balanced accuracy: for binary classification, balanced accuracy equals (recall + specificity) / 2.
Caution: If there are no actual negatives (TN + FP = 0), specificity is undefined. Also, changing the classification threshold may change both specificity and recall. Define the positive class before calculating either metric.
In short: specificity tells you how reliably a classifier recognizes actual negatives. Higher specificity means a lower false positive rate.
References
Learning Sequence
Machine Learning Curriculum
Explore Related Questions
Deepen your understanding across related Machine Learning topics.