Sub topic 4
Accuracy: the share of correct predictions
This chapter assumes you know what a confusion matrix looks like and can name its four cells (TP, FP, FN, TN). It also assumes you can read short Python snippets.
What accuracy measures
Accuracy is the fraction of all predictions that were correct. A prediction is correct when the predicted label equals the actual label. In a binary matrix, those are the two cells on the main diagonal: true positives and true negatives.
accuracy = (TP + TN) / (TP + TN + FP + FN)The denominator is the total number of examples. The numerator is the diagonal.
A worked example
Take ten examples where 1 is the positive class. The counts below are TP = 3, TN = 4, FP = 2 and FN = 1. The correct predictions are 3 + 4 = 7 out of 10, so accuracy is 0.7.
from sklearn.metrics import confusion_matrix, accuracy_score
y_true = [1, 0, 1, 1, 0, 0, 1, 0, 0, 0]
y_pred = [1, 0, 0, 1, 0, 1, 1, 1, 0, 0]
cm = confusion_matrix(y_true, y_pred, labels=[0, 1])
print(cm)
# [[4 2]
# [1 3]]
tn, fp, fn, tp = cm.ravel()
print((tp + tn) / (tp + tn + fp + fn)) # 0.7
print(cm.trace() / cm.sum()) # 0.7, diagonal over total
print(accuracy_score(y_true, y_pred)) # 0.7All three lines give the same number. The trace() version is the one that carries over to more than two classes: sum the diagonal, then divide by the sum of every cell.
Properties worth knowing
- Accuracy does not depend on axis orientation. Transposing the matrix swaps FP and FN but leaves the diagonal alone. You still need to check the axes before reading any other metric.
- Accuracy treats both error types as equally costly. A false alarm and a missed positive each subtract one from the numerator.
- Accuracy ignores which class the errors fall in. A model can be wrong only on the positive class and still score well if that class is rare.
1 - accuracyis the error rate, the share of off-diagonal counts.
Common errors
- Counting only true positives as correct. True negatives are correct predictions too, and they belong in the numerator.
- Reading accuracy without the class balance. Check the row sums first. If one actual class makes up 95% of the data, then 95% accuracy is what you get by always predicting that class.
- Comparing accuracy across different datasets. A score of 0.9 on a balanced test set and 0.9 on a skewed one mean different things.
- Reporting accuracy alone when error costs differ. In screening or fraud detection, a missed positive usually costs far more than a false alarm. Accuracy cannot show that.
When accuracy is a reasonable choice
Accuracy works as a headline number when the classes are roughly balanced and the two error types cost about the same. It is also a useful sanity check against a baseline. Compare your model with the accuracy of always predicting the most common class. If you cannot beat that baseline, the model has learned little.
Exercise
A binary classifier produces TP = 40, FP = 10, FN = 20 and TN = 130. What is its accuracy, and what would its accuracy be if it always predicted the negative class?
Show answer
Accuracy is (40 + 130) / (40 + 10 + 20 + 130) = 170 / 200 = 0.85. There are 150 actual negatives (130 + 20 is wrong; actual negatives are TN + FP = 140) and 60 actual positives (TP + FN), so check the total: 140 + 60 = 200. Always predicting negative is correct on the 140 actual negatives, so its accuracy is 140 / 200 = 0.70. The model beats this baseline by 15 percentage points.