A confusion matrix is a compact table that shows how a classification model's predictions compare with the true labels. It is especially useful when a model must sort cases into two groups, such as positive and negative, pass and fail, or disease and no disease. Instead of giving only one accuracy score, the matrix shows the different ways a model can be right or wrong.
This matters because different mistakes can have very different costs in medicine, security, finance, and science.
Key Facts
- Total cases = TP + TN + FP + FN
- Accuracy = (TP + TN) / (TP + TN + FP + FN)
- Precision = TP / (TP + FP)
- Recall = Sensitivity = TP / (TP + FN)
- Specificity = TN / (TN + FP)
- F1 score = 2TP / (2TP + FP + FN)
Vocabulary
- Confusion matrix
- A table that counts correct and incorrect predictions for each actual class in a classification problem.
- True positive
- A case that is actually positive and is predicted positive by the model.
- False positive
- A case that is actually negative but is incorrectly predicted positive by the model.
- False negative
- A case that is actually positive but is incorrectly predicted negative by the model.
- Precision
- The fraction of predicted positive cases that are actually positive.
Common Mistakes to Avoid
- Swapping false positives and false negatives: a false positive is predicted positive when the actual class is negative, while a false negative is predicted negative when the actual class is positive.
- Reading the axes backward: always check which direction shows actual class and which direction shows predicted class before identifying TP, TN, FP, and FN.
- Using accuracy alone on imbalanced data: high accuracy can hide poor performance on the smaller or more important class.
- Treating precision and recall as the same measure: precision asks how many predicted positives were correct, while recall asks how many actual positives were found.
Practice Questions
- 1 A test has TP = 40, TN = 50, FP = 10, and FN = 20. Find the total number of cases, accuracy, precision, and recall.
- 2 A classifier checks 200 emails for spam. It has 70 true positives, 100 true negatives, 20 false positives, and 10 false negatives. Calculate specificity and F1 score.
- 3 In a disease screening test, explain whether a false positive or a false negative is usually more dangerous, and justify your answer using the meaning of each error.