Skip to main content

How to Plot a Confusion Matrix in scikit-learn

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

A confusion matrix lays out every answer a classifier gave: which true class each case had, and which class the model said. The four cells for a yes/no problem are the true negatives, false positives, false negatives and true positives, which precision, recall and accuracy are all built from. This guide prints one for a medical test and draws it with scikit-learn, with real output from scikit-learn 1.9.1.

Syntax​

Syntax
from sklearn.metrics import ConfusionMatrixDisplay, confusion_matrix

counts = confusion_matrix(y_true, y_pred)
ConfusionMatrixDisplay.from_predictions(y_true, y_pred, normalize="true")
PartWhat it means
y_true, y_predThe true classes and the predicted ones, in the same order
confusion_matrixReturns the counts: one row per true class, one column per predicted class
from_predictionsDraws the matrix with Matplotlib
normalize="true"Divides each row by its total, so each cell shows the share of that true class. None, the default, keeps the counts

The reference for confusion_matrix defines the cell in row i and column j as "the number of observations known to be in group i and predicted to be in group j". So for two classes, the true negatives are top left, the false positives top right, the false negatives bottom left and the true positives bottom right.

Step 1: Count and Draw​

Ten patients, four of them ill (1), and what a test said about each one:

The confusion_matrix_demo.py script in a macOS-style code window: ten true labels and the test's answers, the confusion matrix printed, then drawn with ConfusionMatrixDisplay.from_predictions normalized over the true labels and saved as confusion_matrix.png.

confusion_matrix_demo.py
import matplotlib.pyplot as plt
from sklearn.metrics import ConfusionMatrixDisplay, confusion_matrix

# Ten patients, four of them ill (1), and what a test said about each one.
actual = [1, 1, 1, 1, 0, 0, 0, 0, 0, 0]
predicted = [1, 1, 1, 0, 1, 1, 0, 0, 0, 0]

print(confusion_matrix(actual, predicted))

ConfusionMatrixDisplay.from_predictions(actual, predicted, normalize="true")
plt.savefig("confusion_matrix.png")
Terminal
uv run python confusion_matrix_demo.py
Output
[[4 2]
[1 3]]

The output of confusion_matrix_demo.py in a terminal: the matrix with 4 and 2 in the first row and 1 and 3 in the second.

  • First row, the six healthy patients: 4 cleared, 2 told they were ill (false positives).
  • Second row, the four ill patients: 1 missed (a false negative), 3 found.

The script saved the drawing as confusion_matrix.png:

The saved confusion_matrix.png: a two-by-two grid with true label on the left and predicted label at the bottom, the first row reading 0.67 and 0.33 and the second 0.25 and 0.75, coloured from dark purple to yellow.

With normalize="true", each row adds up to 1: the test cleared 0.67 of the healthy patients and found 0.75 of the ill ones. The bottom-right cell is the recall, and the top-left one is the share of healthy patients cleared.

Common Mistakes​

MistakeWhat happensDo this instead
Reading columns as the true classesEvery cell gets the wrong meaningRows are the true classes, columns the predictions
Comparing raw counts across classes of different sizesThe big class's numbers dominatenormalize="true"

Troubleshooting​

SymptomLikely causeFix
No window opensThe script saves the figure instead of showing itOpen confusion_matrix.png, or call plt.show()
The matrix has one row and one columnOnly one class among the answersScore more cases, or pass labels= with every class

FAQ​

Can I label the classes with words?​

Yes: pass display_labels=["healthy", "ill"] to from_predictions.

Does MLflow draw this for me?​

Yes: mlflow.models.evaluate logs a normalized confusion matrix for every classifier it evaluates: How to Evaluate a Classification Model with mlflow.models.evaluate.

Conclusion​

confusion_matrix counted the test's answers, [[4 2] [1 3]], and ConfusionMatrixDisplay.from_predictions(..., normalize="true") drew them as the share of each true class: 0.67 of the healthy patients cleared, 0.75 of the ill ones found.

Additional Resources​