The confusion matrix

Count every kind of right and wrong answer in one table, derive precision and recall from it, and read where a multiclass model confuses one class with another.

Lesson 36 of 6610 minBeginner

You will learn to

  • Read true/false positives and negatives
  • Derive precision and recall from the matrix
  • Read a multiclass confusion matrix

Before this lesson

Intuition

A single score hides what kind of mistakes a model makes. Imagine a teacher who gives a class one grade, "81 percent". It tells you nothing about which students struggled with which topics. A marking table that lists, for each real answer, what the student wrote instead, tells you much more.

The confusion matrix is that table for a classifier. It has one row per true class and one column per predicted class. Each cell counts how many rows have that true class and got that prediction. Correct predictions sit on the diagonal. Everything off the diagonal is a mistake, and its position tells you which mistake: the row says what it really was, the column says what the model claimed.

Picture it

With two classes there are four cells, and each has a name. Calling one class "positive" (the one you want to find), the names are true positive (TP), false negative (FN), false positive (FP) and true negative (TN). The first word says whether the model was right; the second says what it predicted.

With more classes the idea is the same, only bigger. Here is a three-class example with 100 images.

Predicted classTrue classCatCatDogDogBirdBird426252231217
Confusion matrix for 100 animal photos. The solid outline is the Dog row (recall reads along it). The dashed outline is the Dog column (precision reads down it).

The math

For a class cc, let nijn_{ij} be the cell in row ii (true class) and column jj (predicted class). Then:

recallc=ncc∑jncjprecisionc=ncc∑inicaccuracy=∑cncc∑i,jnij\text{recall}_c = \frac{n_{cc}}{\sum_j n_{cj}} \qquad \text{precision}_c = \frac{n_{cc}}{\sum_i n_{ic}} \qquad \text{accuracy} = \frac{\sum_c n_{cc}}{\sum_{i,j} n_{ij}}

In words: recall is the diagonal cell divided by its row total, precision is the diagonal cell divided by its column total, and accuracy is the sum of the diagonal divided by everything. In the two-class case, the row total of the positive class is TP + FN and its column total is TP + FP, which gives the formulas from the previous lesson.

Worked example

Take the matrix in the figure. Rows are the true class, columns the prediction:

True \ PredictedCatDogBirdRow total
Cat426250
Dog522330
Bird121720
Column total483022100
  • Accuracy = (42 + 22 + 17) / 100 = 0.81.
  • Recall: Cat 42 / 50 = 0.84, Dog 22 / 30 = 0.733, Bird 17 / 20 = 0.85.
  • Precision: Cat 42 / 48 = 0.875, Dog 22 / 30 = 0.733, Bird 17 / 22 = 0.773.
  • F1 (using 2 × diagonal / (row total + column total)): Cat 84 / 98 = 0.857, Dog 44 / 60 = 0.733, Bird 34 / 42 = 0.810.

Macro F1 is the plain mean, (0.857 + 0.733 + 0.810) / 3 = 0.80. The weighted F1 weights by row totals: (50 × 0.857 + 30 × 0.733 + 20 × 0.810) / 100 = 0.81.

What does the matrix say that the scores do not? Dog is the weak class on both sides. The model misses 8 real dogs (5 called cats, 3 called birds), and 8 other animals are wrongly called dogs (6 cats, 2 birds). The biggest off-diagonal cell is 6 cats predicted as dogs, so the first thing to look at is what makes cats and dogs look alike in your features. That is a much more useful next step than "improve accuracy".

How mlflow does it

On a model candidate page, the Plots tab offers Confusion matrix for classification runs. It is computed on the held-out test rows, which the model never trained on, and shows counts as a heatmap with the true class on the vertical axis and the predicted class on the horizontal axis. Hover over a cell to see the true class, the predicted class and the count. Note that classes appear in sorted order, so check the axis labels to find which class is "positive". See Read a run for the other plots.

Common mistakes

  • Reading rows and columns the wrong way round. Some tools put the true class on the columns. Always check the axis titles before computing precision or recall.
  • Judging by raw counts when classes have very different sizes. A big class always has bigger numbers. Divide by the row total to compare recall between classes.
  • Looking only at the diagonal. A high diagonal can still hide a class with poor recall. Check each row.
  • Building the matrix from training rows. The model has already seen them, so the matrix looks too clean. Use held-out rows or cross-validated predictions.
  • Ignoring which mistakes matter. Ten false negatives and ten false positives are rarely equally costly. Say which cell you want to shrink before you tune the model.

Try it in mlflow

Open the confusion matrix plot

  1. Open Runs, open a finished classification run and click a model candidate.
  2. Open the Plots tab and choose Confusion matrix.
  3. Find the biggest number that is not on the diagonal. Those are the two classes the model mixes up most.
  4. Compute the recall of one class (its diagonal cell divided by its row total) and compare it with the leaderboard.
Open in mlflow

Read in the docs

Check your understanding

  1. 1. In a confusion matrix with true classes as rows and predicted classes as columns, where do you read the recall of a class?
  2. 2. A cell outside the diagonal holds 12 in the row 'Dog' and the column 'Cat'. What does it mean?
  3. 3. For a class, precision is much lower than recall. What does that say about the model?