Coding Now – Best AI & Full Stack Courses in Delhi NCR | 100% Placement
Limited Offer: Get 50% OFF on AI & Full Stack Courses
📞 Call Now: +91 9667708830
Back to Deep Learning Notes
Topic #337

Specificity

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

\[ \text{Specificity} = \frac{TN}{TN+FP} \]

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):

\[ \text{Specificity} = \frac{63}{63+7} = \frac{63}{70} = 0.9 \]

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.

Related DL Notes

Want to go beyond the notes?

Join Coding Hubs School of AI's Deep Learning course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available
💬 Talk to Advisor
1
WhatsApp

Latest from Our Blog

Insights on AI, Data Science, Full Stack & Career

View All Articles →