4.3.7
Sensitivity / Specificity
Summary
- Understand the fundamentals of this metric, what it evaluates, and how to interpret the results.
- Compute and visualise the metric with Python 3.13 code examples, covering key steps and practical checkpoints.
- Combine charts and complementary metrics for effective model comparison and threshold tuning.
- Confusion Matrix — understanding this concept first will make learning smoother
1. Definition #
From the confusion matrix we can write:
$$ \mathrm{Sensitivity} = \frac{TP}{TP + FN}, \qquad \mathrm{Specificity} = \frac{TN}{TN + FP} $$- Higher sensitivity means fewer missed positives.
- Higher specificity means fewer negatives flagged incorrectly.
2. Computing in Python 3.13 #
| |
| |
In scikit-learn, sensitivity is the standard recall. Specificity can be obtained via recall_score(y_true, y_pred, pos_label=0) or by computing it manually as above.
3. Threshold trade-offs #
For probabilistic models, adjusting the decision threshold changes sensitivity and specificity.
| |
- Each point on the ROC curve corresponds to a specific sensitivity/specificity pair.
- When misclassification costs are known, optimise the threshold using metrics such as the Youden Index or cost-sensitive objectives.
4. Youden Index for balanced choices #
The Youden Index balances the two metrics:
$$ J = \mathrm{Sensitivity} + \mathrm{Specificity} - 1 $$The threshold that maximises \(J\) offers a good compromise when both types of errors matter.
5. Practical guidance #
- Sensitivity-first: Screening for severe diseases values sensitivity to avoid missing positive cases.
- Specificity-first: Fraud detection systems may favour specificity to reduce costly false positives on legitimate transactions.
- Reporting: Present sensitivity and specificity alongside Accuracy so stakeholders can judge trade-offs explicitly.
Key takeaways #
- Sensitivity tracks missed positives; specificity tracks false alarms on negatives.
- Moving the decision threshold trades one for the other—align the balance with business costs.
- Use tools like the Youden Index to complement Accuracy and surface risks that a single metric would hide.