Sub topic 7
Why accuracy misleads on imbalanced data
This chapter assumes you can read a binary confusion matrix and name its four cells (TP, FP, FN, TN). It also assumes you know the definitions of accuracy and recall from the earlier chapters.
The accuracy trap
Accuracy is the share of all predictions that are correct: (TP + TN) / (TP + TN + FP + FN). It treats every example equally. When one class makes up most of the data, the majority class dominates the numerator, and accuracy mostly measures how well the model handles that class.
Class imbalance means the classes appear in very different proportions. Fraud detection, disease screening and defect detection are typical cases. The rare class is usually the one you care about.
A model that finds nothing
Take 100 examples: 95 negative and 5 positive. A model that always predicts "negative" is right on every negative and wrong on every positive.
from sklearn.metrics import confusion_matrix, accuracy_score, recall_score
y_true = [0] * 95 + [1] * 5
y_pred = [0] * 100 # a model that always predicts the negative class
print(confusion_matrix(y_true, y_pred, labels=[0, 1]))
# [[95 0]
# [ 5 0]]
print(accuracy_score(y_true, y_pred)) # 0.95
print(recall_score(y_true, y_pred)) # 0.0The matrix shows TN = 95, FP = 0, FN = 5 and TP = 0. Accuracy is 95 / 100 = 0.95. Recall is TP / (TP + FN) = 0 / 5 = 0. The model scores 95% and never finds a single positive.
Spotting it in the matrix
The accuracy number hides the problem, but the matrix does not. Check these things before trusting an accuracy score:
- Sum each row. A very uneven split between the row totals tells you the data is imbalanced.
- Look at the row of the rare class. If most of its count sits in the FN cell, recall is low.
- Look at the column of the positive prediction. If it is nearly empty, the model rarely predicts the positive class at all.
Baselines and better metrics
A useful habit is to compute the majority-class baseline: the accuracy of always predicting the most common class. Here it is 0.95. A model must beat that number before its accuracy means anything. In the example above, a model with 96% accuracy would barely improve on the baseline.
Metrics that focus on the rare class are more informative:
| Metric | What it checks | Value for the always-negative model |
|---|---|---|
| Accuracy | Share of all predictions that are correct | 0.95 |
| Recall | Share of real positives found | 0.0 |
| Precision | Share of positive predictions that are right | Undefined (0 / 0) |
| Specificity | Share of real negatives found | 1.0 |
Precision is undefined because the model makes no positive predictions. scikit-learn returns 0.0 and raises a warning by default, which you can control with the zero_division argument.
Common errors
- Reporting accuracy alone on skewed data. Always report it next to recall and precision for the rare class.
- Assuming a high score means a good model. Compare against the majority-class baseline first.
- Using accuracy to choose between models when the cost of a missed positive is high. Recall or F1 usually reflects that cost better.
Exercise
A dataset has 990 negative and 10 positive examples. A model produces TP = 2, FN = 8, FP = 5, TN = 985. Compute its accuracy and recall, and say whether the accuracy is impressive.
Show answer
Accuracy = (2 + 985) / 1000 = 0.987. Recall = 2 / (2 + 8) = 0.2. The majority-class baseline is 990 / 1000 = 0.99, so the model is slightly worse than always predicting negative. The accuracy is not impressive, and the model misses 80% of the real positives.