Modification of the sklearn.metrics.ConfusionMatrixDisplay class which represents a plot of a confusion matrix, with added matplotlib.text.Text objects for evaluation measures and an auto-positioned colorbar.

API reference

class daze.ConfusionMatrixDisplay(**kwargs)[source]

Confusion Matrix visualization.

It is recommend to use plot_confusion_matrix() to create a ConfusionMatrixDisplay. All parameters are stored as attributes.

confusion_matrixnumpy.ndarray of shape (n_classes, n_classes)

Confusion matrix.

display_labelsnumpy.ndarray of shape (n_classes,), default=None

Display labels for plot. If None, display labels are set from 0 to n_classes - 1.

See also


Plot Confusion Matrix.


>>> from sklearn.datasets import make_classification
>>> from sklearn.metrics import confusion_matrix
>>> from daze 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)
>>> clf.fit(X_train, y_train)
>>> predictions = clf.predict(X_test)
>>> cm = confusion_matrix(y_test, predictions, labels=clf.classes_)
>>> disp = ConfusionMatrixDisplay(confusion_matrix=cm, display_labels=clf.classes_)
>>> disp.plot() 

Image representing the confusion matrix.

text_numpy.ndarray (dtype=matplotlib.text.Text) of shape (n_classes, n_classes), or None

Array of matplotlib axes. None if include_values is false.


Axes with confusion matrix.


Figure containing the confusion matrix.

plot(*, include_measures=True, measures='a', 'c', 'p', 'r', 'f1', measures_format=None, include_summary=True, summary_type='macro', include_values=True, values_format=None, cmap='viridis', xticks_rotation='horizontal', ax=None, colorbar=True, normalize=None)[source]

Plot visualization.

include_measuresbool, default=True

Includes measures outside the confusion matrix.

measuresarray-like of str
Controls which measures to display outside the confusion matrix.
Can contain any of:
  • ‘a’ for accuracy,

  • ‘c’ for row/column counts,

  • ‘tp’ for true positives,

  • ‘fp’ for false positives,

  • ‘tn’ for true negatives,

  • ‘fn’ for false negatives,

  • ‘tpr’ for true positive rate,

  • ‘fpr’ for false positive rate,

  • ‘tnr’ for true negative rate,

  • ‘fnr’ for false negative rate,

  • ‘p’ for precision,

  • ‘r’ for recall (same as ‘tpr’),

  • ‘f1’ for F1 score.

Defaults to (‘a’, ‘c’, ‘p’, ‘r’, ‘f1’).

measures_formatstr, default=None

Format specification for values in confusion matrix. If None, the format specification is ‘.3f’.

include_summarybool, default=True

Includes summary values in the corner above the confusion matrix.

summary_type{‘micro’, ‘macro’}, default=’macro’

Type of averaging used for summary measures.

include_valuesbool, default=True

Includes values in confusion matrix.

values_formatstr, default=None

Format specification for values in confusion matrix. If None, the format specification is ‘d’ or ‘.2g’ whichever is shorter.

cmapstr or matplotlib Colormap, default=’viridis’

Colormap recognized by matplotlib.

xticks_rotation{‘vertical’, ‘horizontal’} or float, default=’horizontal’

Rotation of xtick labels.

axmatplotlib axes, default=None

Axes object to plot on. If None, a new figure and axes is created.

colorbarbool, default=True

Whether or not to add a colorbar to the plot.

normalize{‘true’, ‘pred’, ‘all’}, default=None

Normalizes confusion matrix over the true (rows), predicted (columns) conditions or all the population. If None, confusion matrix will not be normalized.