§ 2.7Module 2

Confusion Matrix

On this page

2.7 — Confusion Matrix

Recall first. For a disease screen, which is more dangerous: a false positive or a false negative? The answer depends on the action and cost—state a reason before reading.

Four outcomes, not one accuracy number

For binary classification, compare predicted class with the true class:

Predicted positivePredicted negative
Actual positiveTP true positiveFN false negative
Actual negativeFP false positiveTN true negative

The confusion matrix is the count table [ [TN, FP], [FN, TP] ] under the common negative-first convention. Always label axes because libraries and textbooks may order classes differently. scikit-learn defines its matrix with rows as true classes and columns as predicted classes.1

From it:

N = TP + TN + FP + FN
accuracy = (TP + TN)/N
error rate = (FP + FN)/N

Accuracy is useful when classes and error costs are reasonably balanced. It can be misleading for rare positives: predicting every case negative may be highly accurate but useless.

Worked calculation

Suppose a detector gives TP=36, FN=4, FP=10, TN=50. Total is 100 and the matrix is:

              predicted +   predicted −
actual +          36             4
actual −          10            50

Accuracy is (36+50)/100=0.86, or 86%; error rate is 14%. There are 40 actual positives and 60 actual negatives. This same table will support sensitivity, specificity, precision, recall, F1, and kappa in the next topics.

Why the matrix is a diagnostic tool

The four cells reveal which mistakes occur. A threshold change can move cases between TP/FN and FP/TN, changing the operating point without refitting the model. In multiclass classification, the matrix becomes K×K: diagonal counts are correct; off-diagonal cells show which classes are confused.

Counts must come from an honest held-out or cross-validated prediction. A confusion matrix on training predictions can hide overfitting. For imbalanced data, report the class distribution and several complementary metrics rather than selecting accuracy alone.2

Exercise

A test has 200 cases: TP=18, FN=2, FP=30, TN=150. Find accuracy and the number of actual positives and negatives.

Revealed answer

Accuracy =(18+150)/200=0.84=84%. Actual positives =18+2=20; actual negatives =30+150=180. The positive class is rare, so 84% alone is incomplete.

Exam lens

Draw the 2×2 table first, define every abbreviation, then substitute counts. Mention that row/column orientation must be stated and that accuracy alone can fail under imbalance.

Rapid revision checklist

Key takeaways

Sources

Footnotes

  1. scikit-learn, confusion_matrix API. ↩

  2. Google, Classification: Accuracy, and scikit-learn, classification metrics. ↩