ML0014 Confusion Matrix

What is the confusion matrix?

Answer

A confusion matrix is a table that summarizes the performance of a classification model by comparing its predicted labels against the actual labels. For binary classification it is a 2×2 table with four cells: true positives (correctly predicted positive), false positives (negative predicted as positive), false negatives (positive predicted as negative), and true negatives (correctly predicted negative). Unlike a single scalar metric, it shows not only how many predictions were wrong but which kinds of errors were made. For multi-class problems, the matrix expands into a larger square table where cell (i, j) counts the instances of actual class i predicted as class j; off-diagonal clusters reveal which specific classes the model systematically confuses, guiding model refinement or relabeling.

(1) Structure: Rows are actual classes, columns are predicted classes; the diagonal holds all correct predictions.
(2) Binary Case: The four cells TP/FP/FN/TN feed every classification metric: accuracy, precision, recall, F1.
(3) Multi-Class Case: An n \times n matrix whose off-diagonal hotspots expose systematic class confusions.

Actual \ PredictedPredicted PositivePredicted Negative
Actual PositiveTrue Positives (TP): correct positiveFalse Negatives (FN): missed positive
Actual NegativeFalse Positives (FP): false alarmTrue Negatives (TN): correct negative

Mathematical Formulation:
M_{ij} = \sum_{k=1}^{N} \mathbb{1}\big(y_k = i,\; \hat{y}_k = j\big)
\text{row-normalized:} \quad \tilde{M}_{ij} = \frac{M_{ij}}{\sum_j M_{ij}}

Where:

  • M_{ij} is the count in row i, column j of the confusion matrix.
  • y_k is the true label of sample k, \hat{y}_k its predicted label, and k\in\{1,\ldots,N\} indexes the N samples.
  • \mathbb{1}(\cdot) is the indicator function: 1 when the condition holds, 0 otherwise.
  • \tilde{M}_{ij} is the row-normalized version: row i then shows the per-class recall distribution, which reads better under class imbalance.
Three class confusion matrix heatmap with off-diagonal confusions

Figure 1: A 3-class example: strong diagonal means healthy classification; the bright off-diagonal cell shows the model systematically confuses class 1 with class 2.


Login to view more content


Log in to track your progress

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *