sklearn plot confusion matrix with labels
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
In current scikit-learn, the clean way to plot a confusion matrix with class labels is to use ConfusionMatrixDisplay, not the older plot_confusion_matrix helper. You can create the plot either from predictions you already have or directly from an estimator. The label names are supplied through display_labels.
The Current API
The modern display class supports two common entry points:
- '
ConfusionMatrixDisplay.from_predictions' - '
ConfusionMatrixDisplay.from_estimator'
If you already computed y_pred, use from_predictions. If you have a fitted classifier and test data, from_estimator is convenient.
Example with Labels
That produces a labeled confusion matrix with readable class names instead of numeric indices.
If You Already Have Predictions
Use from_predictions when prediction generation is separate from plotting.
This is especially useful when you want to compare several models using the same stored predictions.
It also makes evaluation code easier to separate from training code. The plotting function no longer needs to know how the model was fit; it only needs the true labels, predicted labels, and optional display names.
Normalized Confusion Matrices
Raw counts are useful, but normalized values often make class imbalance easier to interpret.
Common normalization choices are:
- '
"true": normalize by actual class' - '
"pred": normalize by predicted class' - '
"all": normalize by the total sample count'
Be explicit about which version you are showing. A raw confusion matrix answers “how many examples landed in each cell,” while a normalized one answers “what proportion of a class ended up in each cell.” Those are both useful, but they are not interchangeable.
Why plot_confusion_matrix Is Not the Best Answer Anymore
A lot of older examples use plot_confusion_matrix, but scikit-learn moved toward ConfusionMatrixDisplay as the current display interface. If you copy old snippets blindly, you may run into deprecation warnings or missing helpers depending on your installed version.
So if your goal is current scikit-learn code, use ConfusionMatrixDisplay directly.
Choosing Good Labels
The display_labels argument should match the class order used by the confusion matrix. When your target labels are integers but you want readable names, provide a list in the same class order.
If you are unsure, inspect model.classes_ after fitting.
For binary classification, do not stop at the heatmap alone. Read it alongside precision, recall, and class support so you know whether the visually larger cells are just reflecting class imbalance rather than genuinely strong performance.
Common Pitfalls
A common mistake is passing label names in the wrong order. That makes the plot look polished but semantically wrong.
Another mistake is using outdated examples that call deprecated plotting helpers instead of ConfusionMatrixDisplay.
A third issue is reading only the diagonal counts and ignoring class imbalance. A normalized confusion matrix often reveals model weakness more clearly than raw totals.
Summary
- Use
ConfusionMatrixDisplay.from_estimatororfrom_predictionsin current scikit-learn - Pass readable class names through
display_labels - Use normalized plots when raw counts are hard to compare
- Check class order before labeling the axes
- Prefer the current display API over older deprecated confusion-matrix helpers
Related reading
- sklearn roc_auc_score with multi_classovr should have None average available
- Sklearn SGDClassifier partial fit
- Sklearn StratifiedKFold ValueError Supported target types are ''binary'', ''multiclass''. Got ''multilabel-indicator'' instead
- sklearn train_test_split - ValueError Found input variables with inconsistent numbers of samples
- Sort Algorithm - find which chart bar sees different bar
- Sort (order) data frame rows by multiple columns
- sklearn use Pipeline in a RandomizedSearchCV?
- sklearn utils compute_class_weight function for large dataset
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.