Skip to content

← Metrics & Evaluation step 2 of 7

Easy Primitives

Confusion Matrix

A confusion matrix shows which classes a classifier mixes up. Rows represent true labels and columns represent predictions. Entry M[i, j] is the number of examples whose true class is i and predicted class is j. The diagonal contains correct predictions; off-diagonal entries count errors.

For labels [0, 0, 1] and predictions [0, 1, 1], the matrix is [[1, 1], [0, 1]]. One class-0 example was correct and one was called class 1. The only class-1 example was correct.

Summing a row gives the number of true examples of that class. Summing a column gives the number of predictions of that class. All entries together sum to the number of examples.

Your task

Implement confusion_matrix(predictions, labels, num_classes):

  • predictions and labels have the same 1D shape (N,) and contain class IDs, not score vectors or probabilities.
  • num_classes is a positive integer C. All IDs are whole numbers in [0, C - 1]; the test inputs may store them as floating-point values.
  • Return a tensor of shape (C, C) containing counts, without normalization.

Convert IDs to integer indices when needed. For each example k, add one to M[labels[k], predictions[k]]. Repeated pairs must accumulate counts, not overwrite them. Keep rows and columns for every declared class, even if that class never occurs. An empty input returns an all-zero matrix.

A confusion matrix locates patterns of mistakes. It does not by itself tell you whether they come from data, labels, model limitations, or the choice of decision rule.