Sub topic 3
Accuracy: the share of correct predictions
This chapter assumes you can read basic Python, know what a labeled binary classification task is, and have met the confusion matrix (TP, FP, TN, FN) in the previous chapter. You will compute accuracy from those four counts and learn what it does and does not tell you.
What accuracy measures
Accuracy is the fraction of predictions that match the true label. A prediction is correct in two cases: the model said positive and the label is positive (a true positive), or the model said negative and the label is negative (a true negative). Everything else is an error.
accuracy = (TP + TN) / (TP + FP + TN + FN)The denominator is the total number of examples. The numerator is the diagonal of the confusion matrix, the cells where prediction and label agree.
The related error rate is 1 - accuracy. It is the share of predictions that were wrong, which is (FP + FN) / total.
Worked example
Start from ten labeled examples and the model's predictions. Count each confusion matrix cell by comparing the true label with the prediction.
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 1Now apply the formula to those counts.
tp, fp, tn, fn = 3, 1, 5, 1
accuracy = (tp + tn) / (tp + fp + tn + fn)
print(accuracy) # 0.8Three positives and five negatives were correct, so 8 of 10 predictions were right. Accuracy is 0.8, or 80%.
You can also skip the confusion matrix and compare labels directly. This gives the same number and is a useful cross-check.
y_true = [1, 0, 1, 1, 0, 0, 0, 0, 0, 1]
y_pred = [1, 0, 0, 1, 0, 1, 0, 0, 0, 1]
correct = sum(t == p for t, p in zip(y_true, y_pred))
print(correct / len(y_true)) # 0.8What accuracy hides
Accuracy treats every error the same. A false positive and a false negative each lower the score by one example, even when one of them is far more costly in practice. It also does not say which class the errors fall in. Two models can both score 0.8 while one misses most positives and the other raises many false alarms.
Accuracy also depends on how common each class is. Consider 100 transactions with one fraud case. A model that predicts "not fraud" every time gets 99 right.
# 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.0Accuracy is 0.99, yet the model never finds a single fraud case. The second number is recall, which later chapters cover. This failure is called the accuracy paradox, and the next chapter looks at it in detail.
Common errors
- Computing accuracy on the training data. A model can score very high on examples it has already seen. Report accuracy on a held-out test set.
- Dividing by the wrong total. The denominator is all four cells, not just the positives or just the predictions of one class.
- Reporting a percentage and a fraction side by side without saying which is which. 0.8 and 80% are the same value.
- Treating accuracy as the only metric. Use it as a first check, then look at precision and recall.
When accuracy is a reasonable choice
Accuracy works well when the classes are roughly balanced and a false positive costs about the same as a false negative. A model that sorts photos into two equally common categories is a typical case. Even then, check the confusion matrix, because it shows where the errors are.
Exercise
A classifier is tested on 100 examples and produces TP = 40, FP = 10, TN = 45, FN = 5. What are its accuracy and its error rate? If 50 of the 100 examples were not positive, would a model that always predicts negative beat it on accuracy?
Show answer
Accuracy = (40 + 45) / 100 = 0.85, so the error rate is 0.15. Positives are 40 + 5 = 45, so negatives are 55 (45 + 10). An always-negative model would get 55 of 100 right, an accuracy of 0.55, so it does not beat 0.85. The 50-negative framing in the question conflicts with the counts; the counts give 55 negatives.