Sub topic 4
When accuracy misleads: imbalanced classes
This chapter assumes you can read basic Python, know what a labeled binary classification problem is, and have seen a confusion matrix with TP, FP, TN, and FN counts. Accuracy is the share of predictions that are correct, which is (TP + TN) divided by all predictions. It is easy to compute, and on skewed data it can hide a model that does nothing useful.
What class imbalance is
A dataset has class imbalance when one class is much more common than the other. Many real problems look like this:
- Fraud: about 1 transaction in 100 or fewer is fraudulent.
- Disease screening: most people tested do not have the condition.
- Equipment failure: most machines are fine on most days.
In these problems the rare class is usually the one you care about. Call it the positive class (label 1). The common class is the negative class (label 0).
The accuracy paradox
The accuracy paradox is the situation where a model with high accuracy is worse than useless for the task. Take 100 transactions with exactly one fraud case. Consider a model that predicts "not fraud" every time.
# 1 fraud case in 100 transactions; the model predicts "not fraud" every time
y_true = [1] + [0] * 99
y_pred = [0] * 100
correct = sum(t == p for t, p in zip(y_true, y_pred))
print(correct / len(y_true)) # 0.99
tp = 0
fn = 1
print(tp / (tp + fn)) # 0.0The model is right on all 99 legitimate transactions, so accuracy is 0.99. It never finds the single fraud case. The second number, TP / (TP + FN), is recall. It is the share of real positives the model found, and here it is 0.0. A 99% accurate model catches 0% of fraud.
Why it happens
Accuracy counts every correct prediction equally. When 99% of the examples are negative, correct negative predictions dominate the total. Errors on the rare class barely change the score.
This gives you a baseline to compare against. The majority-class baseline always predicts the most common class. Its accuracy equals the share of that class. If your model's accuracy is close to that number, the model may have learned little.
from collections import Counter
y_true = [1] + [0] * 99
majority_share = Counter(y_true).most_common(1)[0][1] / len(y_true)
print(majority_share) # 0.99Any model on this data needs to beat 0.99 to look better than the do-nothing baseline by accuracy, and even then accuracy tells you little about the rare class.
Where the failure shows up
The confusion matrix shows what accuracy hides. For the always-negative model:
| Row | Predicted 0 | Predicted 1 |
|---|---|---|
| Actual 0 | TN = 99 | FP = 0 |
| Actual 1 | FN = 1 | TP = 0 |
The whole positive row is wrong. Accuracy mixes both rows into one number, so the failure disappears. Recall looks only at the positive row, so it exposes the failure.
How imbalance changes your workflow
When classes are imbalanced, adjust how you evaluate:
- Report the class balance of your test set next to every metric.
- Compute the majority-class baseline and compare your model with it.
- Look at the confusion matrix, not only a single score.
- Use precision and recall, which focus on the positive class. The next chapters cover them in detail.
A less extreme case
The paradox is not limited to a model that predicts one class. A model can find a few positives and still look better than it is. Suppose 100 transactions contain 5 frauds, and the model flags 1 of them correctly and nothing else.
tp, fp, tn, fn = 1, 0, 95, 4
accuracy = (tp + tn) / (tp + fp + tn + fn)
recall = tp / (tp + fn)
print(accuracy) # 0.96
print(recall) # 0.2Accuracy is 0.96 while the model misses 4 of 5 frauds. A reader who sees only 96% would assume it works well.
Common mistakes
- Reporting accuracy alone on a dataset where one class is under 10% of the rows.
- Comparing accuracy across datasets with different class balances. A score of 0.95 means different things when the baseline is 0.50 and when it is 0.94.
- Rebalancing the test set to make accuracy look meaningful, then forgetting that real traffic is still skewed. Keep the test set's class balance realistic.
Exercise
A test set has 200 emails, and 10 are spam. A model predicts "not spam" for every email. What are its accuracy and its recall for the spam class?
Show answer
Accuracy is 190 / 200 = 0.95, because all 190 non-spam emails are predicted correctly. Recall is TP / (TP + FN) = 0 / (0 + 10) = 0.0, because none of the 10 spam emails are found. The model looks 95% accurate but catches no spam.
Exercise
Your model scores 0.97 accuracy on a dataset where 96% of the labels are negative. Is that result strong evidence the model is good? What would you check next?
Show answer
No. The majority-class baseline already reaches 0.96, so the model improves on it by only one point. Check the confusion matrix and compute recall and precision for the positive class to see how many real positives it finds and how many of its positive predictions are correct.