Sub topic 1
From Model Scores to Labels
Prerequisites
Assume you know binary classification, confusion-matrix terms, basic Python and NumPy, and predicted probabilities from a model such as logistic regression. A classifier score can be a probability or any value that ranks examples from least to most likely positive.
From Model Scores to Labels
A binary classifier often produces a score instead of a final label. For example, a fraud model might assign scores between 0 and 1. The score expresses how strongly the model supports the positive class.
A decision threshold converts scores into labels:
- If the score is at least the threshold, predict positive, usually written as 1.
- Otherwise, predict negative, usually written as 0.
For example, with a threshold of 0.5, scores [0.2, 0.7, 0.4] become labels [0, 1, 0].
The Decision Threshold
The threshold controls the balance between detecting positives and raising false alarms. Lowering it usually produces more positive predictions. This can find more real positives, but it can also create more false positives.
Raising the threshold usually produces fewer positive predictions. This can reduce false alarms, but it can miss more real positives.
import numpy as np
scores = np.array([0.2, 0.7, 0.4, 0.9])
threshold = 0.5
predicted = (scores >= threshold).astype(int)
print(predicted) # [0 1 0 1]The Confusion Matrix at One Threshold
Compare predicted labels with true labels at a fixed threshold. The results form a confusion matrix:
- True positive (TP): predicted positive and actually positive.
- False positive (FP): predicted positive but actually negative.
- True negative (TN): predicted negative and actually negative.
- False negative (FN): predicted negative but actually positive.
Changing the threshold changes these counts. A ROC curve shows this change across many thresholds.
True Positive Rate and False Positive Rate
The true positive rate (TPR) measures the fraction of actual positives detected:
TPR = TP / (TP + FN)
It is also called recall or sensitivity.
The false positive rate (FPR) measures the fraction of actual negatives incorrectly flagged:
FPR = FP / (FP + TN)
A ROC curve plots TPR against FPR as the threshold sweeps from high to low. Each point represents one threshold and its resulting confusion-matrix rates.
Exercise
A threshold produces TP = 45, FN = 5, FP = 20, and TN = 80. What are the TPR and FPR?
Show answer
TPR = 45 / (45 + 5) = 0.90. FPR = 20 / (20 + 80) = 0.20.