Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Scikit learn Scikit learn ConfusionMatrixDisplay

From Leeroopedia


Knowledge Sources
Domains Machine Learning, Visualization
Last Updated 2026-02-08 15:00 GMT

Overview

Concrete tool for visualizing confusion matrices of classification models provided by scikit-learn.

Description

The ConfusionMatrixDisplay class provides a visualization of the confusion matrix, showing the counts of true vs. predicted labels as a color-coded heatmap. It supports creation from a pre-computed confusion matrix, directly from an estimator and data via from_estimator, or from true and predicted labels via from_predictions. The display can be customized with different color maps, value formats, and label configurations.

Usage

Use this display class when you need to visually inspect the classification performance of a model across all classes, identify which classes are commonly confused, or present classification results in a clear visual format.

Code Reference

Source Location

Signature

class ConfusionMatrixDisplay:
    def __init__(self, confusion_matrix, *, display_labels=None)
    def plot(self, *, include_values=True, cmap="viridis", xticks_rotation="horizontal",
             values_format=None, ax=None, colorbar=True, im_kw=None, text_kw=None)
    @classmethod
    def from_estimator(cls, estimator, X, y, *, labels=None, sample_weight=None,
                       normalize=None, display_labels=None, include_values=True,
                       xticks_rotation="horizontal", values_format=None, cmap="viridis",
                       ax=None, colorbar=True, im_kw=None, text_kw=None)
    @classmethod
    def from_predictions(cls, y_true, y_pred, *, labels=None, sample_weight=None,
                         normalize=None, display_labels=None, include_values=True,
                         xticks_rotation="horizontal", values_format=None, cmap="viridis",
                         ax=None, colorbar=True, im_kw=None, text_kw=None)

Import

from sklearn.metrics import ConfusionMatrixDisplay

I/O Contract

Inputs

Name Type Required Description
confusion_matrix ndarray of shape (n_classes, n_classes) Yes Pre-computed confusion matrix
display_labels ndarray of shape (n_classes,) No Display labels for the plot axes
estimator estimator instance Yes (from_estimator) Fitted classifier
X array-like of shape (n_samples, n_features) Yes (from_estimator) Input data
y array-like of shape (n_samples,) Yes (from_estimator/from_predictions) True labels
y_pred array-like of shape (n_samples,) Yes (from_predictions) Predicted labels
normalize str (true, pred, all) or None No Normalization mode for the confusion matrix
cmap str or matplotlib Colormap No Colormap for the matrix display (default viridis)

Outputs

Name Type Description
display ConfusionMatrixDisplay Display object with im_, text_, ax_, and figure_ attributes

Usage Examples

Basic Usage

import matplotlib.pyplot as plt
from sklearn.datasets import make_classification
from sklearn.metrics import ConfusionMatrixDisplay
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC

X, y = make_classification(random_state=0)
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)

clf = SVC(random_state=0).fit(X_train, y_train)

# Create display directly from estimator
ConfusionMatrixDisplay.from_estimator(clf, X_test, y_test)
plt.show()

# Or from predictions
y_pred = clf.predict(X_test)
ConfusionMatrixDisplay.from_predictions(y_test, y_pred)
plt.show()

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment