Sub topic 3
Specificity and False Positive Rate
Assume you know supervised classification, ratios, and basic Python. A confusion matrix compares actual labels with predicted labels.
Specificity and false positive rate
For binary classification, specificity measures how many actual negatives the model correctly rejects:
- Specificity = TN / (TN + FP)
- False positive rate (FPR) = FP / (TN + FP)
- FPR = 1 − specificity
A false positive is a negative case incorrectly predicted as positive. For example, a medical screening model with high specificity produces few false alarms.
How the decision threshold reshapes the matrix
A classifier often produces a score between 0 and 1. A decision threshold turns that score into a class label. Raising the threshold usually reduces false positives but increases false negatives.
from sklearn.metrics import confusion_matrix
y_true = [1, 0, 1, 1, 0, 0, 1, 0, 0, 0]
scores = [0.9, 0.2, 0.4, 0.8, 0.1, 0.7, 0.6, 0.55, 0.3, 0.35]
for threshold in (0.35, 0.5, 0.65):
y_pred = [int(score >= threshold) for score in scores]
print(threshold, confusion_matrix(y_true, y_pred, labels=[0, 1]))At 0.35, the model finds every positive but raises more false alarms. At 0.65, it raises fewer alarms but misses more positives.
Axis conventions in scikit-learn and other tools
In scikit-learn, rows represent actual labels and columns represent predicted labels. With labels [0, 1], the matrix is arranged as follows:
| Row | Predicted 0 | Predicted 1 |
|---|---|---|
| Actual 0 | TN | FP |
| Actual 1 | FN | TP |
Other tools may reverse an axis or use a different positive-class order. Check the label order and axis documentation before interpreting cells.
Normalizing a matrix
Normalization changes counts into proportions.
- Row normalization (
normalize="true") shows outcomes for each actual class. The diagonal gives per-class recall. - Column normalization (
normalize="pred") shows the composition of each predicted class. The diagonal gives per-class precision. - Total normalization (
normalize="all") shows each cell's share of all examples.
Reading multiclass matrices
A multiclass matrix has one row and column per class. Off-diagonal cells reveal specific confusions. For example, many actual cats predicted as dogs identify a cat–dog error pattern.
Macro averaging gives every class equal weight. Micro averaging pools all decisions before calculating a metric. Weighted averaging gives larger classes more influence according to their support. Use macro to expose minority-class performance and weighted when overall class frequency should matter.
Exercise
If specificity is 0.92, what is the false positive rate?
Show answer
The false positive rate is 1 − 0.92 = 0.08, or 8%.