The plot_confusion_matrix() function is an adjusted version of sklearn.metrics.plot_confusion_matrix() that adds typical confusion matrix measures (see daze.measures) such as true positives, recall and F1 score to the confusion matrix plot, making it more informative.

While much of this information is included in the classification report generated by sklearn.metrics.classification_report(), it can sometimes be useful to combine these measures alongside confusion matrix, in an easy-to-read format.

API reference

daze.plot_confusion_matrix(estimator, X=None, y_true=None, *, labels=None, display_labels=None, sample_weight=None, normalize=None, include_measures=True, measures='a', 'c', 'p', 'r', 'f1', measures_format=None, include_summary=True, summary_type='macro', include_values=True, values_format=None, xticks_rotation='horizontal', cmap='viridis', ax=None, colorbar=True)[source]

Plots a confusion matrix and annotates it with per-class and overall evaluation measures.

estimatorestimator instance or numpy.ndarray of shape (n_classes, n_classes)


  • Fitted classifier or a fitted sklearn.pipeline.Pipeline in which the last estimator is a classifier.

  • Pre-computed confusion matrix.

X{array-like, sparse matrix} of shape (n_samples, n_features), default=None

Input values. Only required if estimator is a classifier object.

y_truearray-like of shape (n_samples,), default=None

Target values. Only required if estimator is a classifier object.

labelsarray-like of shape (n_classes,), default=None

List of labels to index the matrix. This may be used to reorder or select a subset of labels. If None is given, those that appear at least once in y_true or y_pred are used in sorted order.

display_labelsarray-like of shape (n_classes,), default=None

Target names used for plotting. By default, labels will be used if it is defined, otherwise the unique labels of y_true and y_pred will be used.

sample_weightarray-like of shape (n_samples,), default=None

Sample weights.

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.

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.colors.Colormap, default=’viridis’

Colormap recognized by matplotlib.

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

Rotation of xtick labels.

axmatplotlib.axes.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.


See also


Confusion Matrix visualization.


>>> import matplotlib.pyplot as plt  
>>> from daze import plot_confusion_matrix
>>> from sklearn.datasets import make_classification
>>> 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)
>>> plot_confusion_matrix(clf, X_test, y_test)  
>>> plt.show()