Specificity in machine learning is the proportion of actual negative cases that a classifier correctly predicts as negative, calculated as TN / (TN + FP). It is also called the true negative rate (TNR), and in Model evaluation it measures how well a model avoids false alarms on the class that should not trigger a positive prediction.
Specificity is the counterpart of recall, which measures the share of actual positives found. Together the two metrics describe a binary classifier’s behavior on each class separately, independent of how common each class is. The term is unrelated to CSS specificity, which ranks style rules in web development.
How specificity is calculated from the confusion matrix
Specificity is computed from the negative row of the confusion matrix, which contains true negatives (TN) and false positives (FP). The numerator counts negatives the model got right, and the denominator counts all real negatives in the evaluation data. The result ranges from 0 to 1, often reported as a percentage.
The table shows the four cells of a binary confusion matrix and which metrics use them.
| Cell | Meaning | Used in |
|---|---|---|
| True negative (TN) | Negative case predicted negative | Specificity, accuracy |
| False positive (FP) | Negative case predicted positive | Specificity, false positive rate, precision |
| True positive (TP) | Positive case predicted positive | Recall, precision, accuracy |
| False negative (FN) | Positive case predicted negative | Recall, false negative rate |
Because specificity uses only real negatives, it is insensitive to class prevalence: the value does not change if more positive examples are added to the test set. This makes it useful for comparing models across datasets with different class balances, as long as the negative population itself is similar.
Specificity is directly linked to the false positive rate (FPR) by the identity specificity = 1 - FPR. A model with a specificity of 0.95 therefore flags 5 percent of negatives as positive.
Specificity compared with related metrics
Specificity is often confused with precision, because both are hurt by false positives. Precision asks how many predicted positives are correct, while specificity asks how many real negatives were left alone. A model can have high specificity and low precision when negatives vastly outnumber positives.
| Metric | Formula | Question it answers |
|---|---|---|
| Specificity (TNR) | TN / (TN + FP) | How many real negatives were correctly rejected |
| Recall (sensitivity) | TP / (TP + FN) | How many real positives were found |
| Precision | TP / (TP + FP) | How many positive predictions were correct |
| False positive rate | FP / (FP + TN) | How many real negatives were wrongly flagged |
| Balanced accuracy | (Recall + Specificity) / 2 | Average performance across both classes |
In medical diagnostics and screening, the pair sensitivity and specificity is the standard reporting format. In information retrieval and many machine learning papers, the pair precision and recall dominates instead, because the negative class is often huge and poorly defined, such as all irrelevant documents on the web.
The trade-off between specificity and recall
Most classifiers output a score or probability, and a decision threshold converts it into a label. Raising the threshold makes the model more conservative: specificity rises, but recall falls because more true positives are missed. Lowering the threshold does the opposite.
The ROC curve plots recall against the false positive rate, which equals one minus specificity, across all thresholds. The AUC summarizes this curve in one number, but a deployed system runs at a single threshold, so teams must still choose an operating point that matches the cost of each error type.
The following table illustrates how one fraud detection model might behave at three thresholds on the same data.
| Threshold | Specificity | Recall | Practical effect |
|---|---|---|---|
| 0.3 (lenient) | 0.90 | 0.92 | Many legitimate transactions reviewed |
| 0.5 (default) | 0.96 | 0.84 | Balanced review workload |
| 0.8 (strict) | 0.995 | 0.61 | Few false alarms, many missed frauds |
When high specificity matters most
Specificity should be prioritized when false positives are expensive, intrusive or harmful. Each wrongly flagged negative may cost money, time or trust, and at large volumes even a small false positive rate produces thousands of false alarms.
| Application | Cost of a false positive | Typical priority |
|---|---|---|
| Confirmatory medical testing | Unnecessary treatment or anxiety | Very high specificity |
| Spam filtering | Legitimate email lost | High specificity |
| Content moderation | Wrongful removal of user posts | High specificity with human review |
| Initial disease screening | Follow-up test needed | Recall usually prioritized |
| Security intrusion alerts | Analyst fatigue | Balanced, tuned per environment |
A common design pattern is a two-stage system: a first model with high recall catches nearly every positive, and a second model or human check with high specificity confirms them. This mirrors clinical practice, where a sensitive screening test is followed by a specific confirmatory test.
Pitfalls in reporting specificity
Specificity alone can be misleading on imbalanced data. A model that predicts negative for every case reaches a perfect specificity of 1.0 while finding no positives at all. It should always be reported together with recall or as part of balanced accuracy or the Matthews correlation coefficient.
- Ignoring the base rate: with rare positives, even 99 percent specificity can mean most alerts are false.
- Comparing at different thresholds: specificity values are meaningful only at a stated operating point.
- Using a shifted negative population: specificity measured on easy negatives overstates real performance.
- Averaging incorrectly in multiclass tasks: per-class specificity must be macro- or micro-averaged explicitly.
- Reporting without confidence intervals: small negative sets make the estimate unstable.
The simple explanation
Specificity tells you how good a model is at saying no when no is the right answer. Imagine a smoke alarm. A highly specific alarm stays quiet when you make toast.
A low-specificity alarm goes off at every slice of toast. It still catches fires, but people start ignoring it. Specificity counts the quiet moments it got right. It does not tell you whether the alarm catches real fires, so you also need a second number called recall.
How it is used in practice
A hospital data science team builds a model that flags chest X-rays as possibly showing pneumonia. On a test set of 2,000 normal X-rays and 300 pneumonia cases, the model at its default threshold labels 1,880 normal images correctly and wrongly flags 120. Its specificity is 1,880 / 2,000, or 0.94, and its recall is 0.91.
Radiologists say 120 false alarms per 2,000 normal scans would add too much review work. The team raises the threshold, which lifts specificity to 0.98 but lowers recall to 0.83. Because missed pneumonia is dangerous, the team keeps the lower threshold for triage ordering only, so flagged images are read first rather than diagnosed automatically.
The final report lists specificity, recall, confidence intervals and the chosen threshold, so clinicians understand both error types before the tool enters the workflow.