Specificity (also called the true negative rate) completes the picture recall started โ while recall measures how well a model catches actual positives, specificity measures how well it correctly identifies actual negatives.
Formula
Compare directly to recall (\(\frac{TP}{TP+FN}\)): recall is "of the actual positives, how many were caught?"; specificity is "of the actual negatives, how many were correctly identified as negative?" They're structurally parallel, mirrored formulas, one for each class.
Numerical Example
Continuing the spam example (TN=63, FP=7):
90% of legitimate (non-spam) emails were correctly left alone.
Why Specificity Matters Alongside Recall
Recall alone can't tell you how the model treats the negative class โ a model that predicts "positive" for absolutely everything achieves perfect recall (\(=1.0\)) trivially, by never missing a true positive, but has terrible specificity (\(=0\), since it also never correctly identifies any negative). Reporting both together prevents this kind of gaming of just one metric.
Code
from sklearn.metrics import confusion_matrix
y_true = [1,1,1,1,1,0,0,0,0,0]
y_pred = [1,1,1,0,0,0,0,1,0,0]
tn, fp, fn, tp = confusion_matrix(y_true, y_pred).ravel()
specificity = tn / (tn + fp)
print(specificity)
Specificity's Role in the ROC Curve
Specificity is closely tied to the false positive rate, \(\text{FPR} = 1-\text{Specificity} = \frac{FP}{FP+TN}\) โ this exact quantity is the x-axis of the ROC curve, covered in the next note, making specificity (via its complement) a direct building block for that visualization.
Common Mistakes
- Confusing specificity with precision โ specificity is about correctly identifying actual negatives (\(\frac{TN}{TN+FP}\)); precision is about how trustworthy positive predictions are (\(\frac{TP}{TP+FP}\)) โ different denominators, different questions entirely.
- Reporting recall alone as if it fully characterizes a binary classifier โ without specificity (or precision) alongside it, recall can be trivially maximized by a model that predicts positive for everything.
Interview Relevance
Q: "What's the difference between recall and specificity, and why report both?" Recall measures how well the model catches actual positives (\(\frac{TP}{TP+FN}\)); specificity measures how well it correctly identifies actual negatives (\(\frac{TN}{TN+FP}\)). A model can trivially achieve perfect recall by predicting positive for everything, but would then have zero specificity โ reporting both prevents either metric from being gamed in isolation and gives a complete picture of performance on both classes.
Practice Question
Using the medical diagnosis confusion matrix (TP=45, FN=5, FP=15, TN=135), compute specificity.