Sub topic 1
Predictions versus True Labels
Prerequisites
Assume you know basic Python, supervised classification, train/test splits, and ratios. A classifier predicts a class, such as fraud or not fraud. A true label is the known class in the data. Evaluation compares each prediction with its matching true label.
The Confusion Matrix: TP, FP, TN, FN
A confusion matrix counts prediction outcomes. For binary classification, treat class 1 as positive and class 0 as negative.
| Actual class | Predicted positive | Predicted negative |
|---|---|---|
| Positive | True positive (TP) | False negative (FN) |
| Negative | False positive (FP) | True negative (TN) |
- TP: The model predicts positive, and the label is positive.
- FP: The model predicts positive, but the label is negative.
- TN: The model predicts negative, and the label is negative.
- FN: The model predicts negative, but the label is positive.
y_true = [1, 0, 1, 1, 0, 0, 0, 0, 0, 1]
y_pred = [1, 0, 0, 1, 0, 1, 0, 0, 0, 1]
tp = sum(t == 1 and p == 1 for t, p in zip(y_true, y_pred))
fp = sum(t == 0 and p == 1 for t, p in zip(y_true, y_pred))
tn = sum(t == 0 and p == 0 for t, p in zip(y_true, y_pred))
fn = sum(t == 1 and p == 0 for t, p in zip(y_true, y_pred))
print(tp, fp, tn, fn) # 3 1 5 1Accuracy: The Share of Correct Predictions
Accuracy is the fraction of all predictions that are correct:
\[ \text{accuracy} = \frac{TP + TN}{TP + FP + TN + FN} \]
Here, accuracy is (3 + 5) / 10 = 0.8, or 80%.
When Accuracy Misleads: Imbalanced Classes
Class imbalance occurs when one class is much more common than another. A model that predicts “not fraud” for every one of 100 transactions gets 99% accuracy when only one transaction is fraudulent. However, it finds no fraud cases.
Exercise
A model has TP=8, TN= eighty, FP=2, and FN=10. What is its accuracy?
Show answer
Accuracy is (8 + 80) / 100 = 0.88, or 88%.