plot_confusion_matrix#

scikitplot.api.metrics.plot_confusion_matrix(y_true, y_pred, labels=None, true_labels=None, pred_labels=None, title=None, normalize=False, hide_zeros=False, hide_counts=False, x_tick_rotation=0, ax=None, fig=None, figsize=None, cmap='Blues', title_fontsize='large', text_fontsize='medium', show_colorbar=True)[source]#

Generates a confusion matrix plot from predictions and true labels.

The confusion matrix is a summary of prediction results that shows the counts of true and false positives and negatives for each class. This function also provides options for normalizing, hiding zero values, and customizing the plot appearance.

Parameters:
y_truearray-like, shape (n_samples,)

Ground truth (correct) target values.

y_predarray-like, shape (n_samples,)

Estimated targets as returned by a classifier.

labelsarray-like, shape (n_classes), optional

List of labels to index the matrix. This may be used to reorder or select a subset of labels. If None, labels appearing at least once in y_true or y_pred are used in sorted order. (new in v0.2.5)

true_labelsarray-like, optional

The true labels to display. If None, all labels are used.

pred_labelsarray-like, optional

The predicted labels to display. If None, all labels are used.

titlestring, optional

Title of the generated plot. Defaults to “Confusion Matrix” if normalize is True. Otherwise, defaults to “Normalized Confusion Matrix”.

normalizebool, optional, default=False

If True, normalizes the confusion matrix before plotting.

hide_zerosbool, optional, default=False

If True, cells containing a value of zero are not plotted.

hide_countsbool, optional, default=False

If True, counts are not overlaid on the plot.

x_tick_rotationint, optional, default=0

Rotates x-axis tick labels by the specified angle. Useful when labels overlap.

axmatplotlib.axes.Axes, optional

The axes upon which to plot the confusion matrix. If None, a new set of axes is created. Axes like fig.add_subplot(1, 1, 1) or plt.gca()

figsizetuple of int, optional

Tuple denoting figure size of the plot, e.g., (6, 6). Defaults to None.

cmapNone, str or matplotlib.colors.Colormap, optional, default=None

Colormap used for plotting. Options include ‘viridis’, ‘PiYG’, ‘plasma’, ‘inferno’, ‘nipy_spectral’, etc. See Matplotlib Colormap documentation for available choices.

title_fontsizestring or int, optional, default=”large”

Font size for the plot title. Use “small”, “medium”, “large”, or integer values.

text_fontsizestring or int, optional, default=”medium”

Font size for text in the plot. Use “small”, “medium”, “large”, or integer values.

show_colorbarbool, optional, default=True

If False, the colorbar is not displayed.

Added in version 0.3.9.

Returns:
axmatplotlib.axes.Axes

The axes on which the plot was drawn.

Notes

Ensure that y_true and y_pred have the same shape and contain valid class labels. The normalize parameter applies only to the confusion matrix plot. Adjust cmap and x_tick_rotation to customize the appearance of the plot. The show_colorbar parameter controls whether a colorbar is displayed.

Examples

>>> from sklearn.datasets import load_digits as data_10_classes
>>> from sklearn.model_selection import train_test_split
>>> from sklearn.naive_bayes import GaussianNB
>>> import scikitplot as skplt
>>> X, y = data_10_classes(return_X_y=True, as_frame=False)
>>> X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.5, random_state=0)
>>> model = GaussianNB()
>>> model.fit(X_train, y_train)
>>> y_val_pred = model.predict(X_val)
>>> skplt.metrics.plot_confusion_matrix(
>>>     y_val, y_val_pred,
>>> );

(Source code, png)

Confusion Matrix