๐Ÿ”ฅLimited Offer: Get 50% OFFon AI & Full Stack Courses๐Ÿ”ฅ
Back to Deep Learning Notes
Topic #148

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.

Want to go beyond the notes?

Join CodingNow 2.0's Deep Learning course โ€” live mentorship, real projects, and 100% placement support.

Enroll Now โ€” Free Demo Available

Specificity โ€“ FAQs

Quick answers about learning Specificity in Deep Learning.

This free note from CodingNow 2.0 explains Specificity in Deep Learning โ€” concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Deep Learning topic on CodingNow 2.0, including Specificity, is 100% free with no signup required.
With focused practice, most students grasp Specificity in 1โ€“3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) โ€” expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now