Data Science Confusion Matrix
A confusion matrix breaks classifier predictions into true positives, false positives, false negatives, and true negatives, forming the basis for accuracy, precision, and recall.
What a Confusion Matrix Tells You
For a binary classifier, every prediction falls into exactly one of four buckets depending on what the model predicted and what actually happened. A confusion matrix arranges counts of these four outcomes into a simple grid, giving you far more detail than a single accuracy number can.
- True Positive (TP): the model predicted positive, and the actual label was positive. A correct positive detection.
- False Positive (FP): the model predicted positive, but the actual label was negative. A false alarm.
- False Negative (FN): the model predicted negative, but the actual label was positive. A missed detection.
- True Negative (TN): the model predicted negative, and the actual label was negative. A correct rejection.
Deriving Accuracy, Precision, and Recall
Accuracy is the fraction of all predictions that were correct: accuracy = (TP + TN) / (TP + TN + FP + FN). It treats every correct prediction the same way, regardless of whether it was a positive or negative case.
Precision asks, of everything the model flagged as positive, how much was actually positive: precision = TP / (TP + FP). Recall asks, of everything that was actually positive, how much did the model catch: recall = TP / (TP + FN). The two metrics trade off against each other: a model can boost recall by flagging more cases as positive, but that usually drags precision down by adding more false positives, and vice versa.
Example
from sklearn.metrics import confusion_matrix, classification_report
y_actual = [1, 0, 1, 1, 0, 1, 0, 0, 1, 0]
y_predicted = [1, 0, 0, 1, 0, 1, 1, 0, 1, 0]
cm = confusion_matrix(y_actual, y_predicted)
print('Confusion matrix:\n', cm)
print(classification_report(y_actual, y_predicted, digits=3))When you need a single number that balances both concerns, the F1 score is the harmonic mean of precision and recall: F1 = 2 * (precision * recall) / (precision + recall). Because it is a harmonic mean rather than a simple average, F1 stays low if either precision or recall is low, so a model cannot game it by excelling at only one of the two.
- The four confusion matrix outcomes are TP, FP, FN, and TN.
- Accuracy = (TP + TN) / total; precision = TP / (TP + FP); recall = TP / (TP + FN).
- F1 score balances precision and recall into a single metric.
- Prefer precision, recall, or F1 over raw accuracy when classes are imbalanced.
Exercise: Data Science Confusion Matrix
What does a confusion matrix summarize?