Notes

Specificity

When a classifier says “positive” or “negative,” the most painful mistakes usually aren’t evenly distributed. Specificity focuses on one particular kind of mistake: how well your model avoids false alarms.

What specificity measures

Specificity (also called the true negative rate) measures the fraction of actual negatives that the model correctly predicts as negative. Using the confusion matrix terms:

  • TN = true negatives (correctly predicted negative)
  • FP = false positives (predicted positive, actually negative)

The formula is:

specificity = TN / (TN + FP)

High specificity means: “When the truth is negative, I rarely cry wolf.” It complements sensitivity (a.k.a. recall or true positive rate), which measures how well you catch positives.

Why it matters in supervised learning

Specificity becomes crucial when false positives are costly or disruptive. For example:

  • Disease screening: a low-specificity test flags many healthy people, triggering anxiety and follow-up procedures.
  • Fraud detection: low specificity means many legitimate transactions get blocked.
  • Spam filtering: low specificity can send real customer emails to spam.

In many models, you can trade specificity against sensitivity by changing the decision threshold on predicted probabilities.

How you’ll see it in practice

In scikit-learn, specificity isn’t a built-in named metric, but it’s easy to compute from the confusion matrix:

from sklearn.metrics import confusion_matrix
tn, fp, fn, tp = confusion_matrix(y_true, y_pred).ravel()
specificity = tn / (tn + fp)

It’s also directly related to the ROC curve: specificity = 1 − false positive rate.

Specificity (true negative rate) is the proportion of actual negative cases that a classifier correctly labels as negative: TN/(TN+FP). It quantifies how well a model avoids false positives, complementing sensitivity/recall for the positive class. Specificity matters when false alarms are costly (e.g., screening tests), and it is a key component of ROC analysis and threshold selection.

Imagine a smoke alarm. A good one should go off when there’s a real fire, but it also shouldn’t scream every time you make toast. Specificity is the “doesn’t cry wolf” part.

In AI classification (when a model sorts things into categories like “spam” vs “not spam” or “disease” vs “healthy”), specificity measures how well the model correctly identifies the negative cases. In other words: out of all the times the correct answer is “no,” how often does the model say “no” too?

High specificity means fewer false alarms (fewer false positives).