0

I am trying to properly display my confusion matrix for a multilabel classification. My approach to achieve that is following:

from sklearn.metrics import multilabel_confusion_matrix, accuracy_score

ypred = res
ytrue = np.argmax(y_test, axis=1).tolist()
ypred = np.argmax(ypred, axis=1).tolist()

multilabel_confusion_matrix(ytrue, ypred)
array([[[171,   0],
        [  0,   8]],

       [[173,   1],
        [  0,   5]],

       [[171,   0],
        [  0,   8]],

       [[173,   0],
        [  1,   5]],

       [[172,   0],
        [  0,   7]],

       [[171,   0],
        [  0,   8]],

       [[177,   0],
        [  0,   2]],

       [[170,   0],
        [  0,   9]],

       [[172,   0],
        [  0,   7]],

       [[174,   0],
        [  0,   5]],

       [[170,   1],
        [  0,   8]],

       [[178,   0],
        [  0,   1]],

       [[169,   2],
        [  0,   8]],

       [[169,   1],
        [  1,   8]],

       [[174,   0],
        [  0,   5]],

       [[172,   0],
        [  1,   6]],

       [[173,   0],
        [  0,   6]],

       [[174,   0],
        [  1,   4]],

       [[169,   0],
        [  0,  10]],

       [[172,   0],
        [  0,   7]],

       [[172,   1],
        [  1,   5]],

       [[171,   0],
        [  0,   8]],

       [[171,   0],
        [  1,   7]],

       [[174,   0],
        [  0,   5]],

       [[174,   0],
        [  0,   5]],

       [[171,   0],
        [  0,   8]],

       [[171,   0],
        [  0,   8]]], dtype=int64)

from sklearn.metrics import confusion_matrix
cm = confusion_matrix(ytrue,ypred)

It gives me the following output:

array([[ 8,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
         0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0],
       [ 0,  5,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
         0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0],
       [ 0,  0,  8,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
         0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0],
       [ 0,  0,  0,  5,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
         0,  0,  0,  0,  1,  0,  0,  0,  0,  0,  0],
       [ 0,  0,  0,  0,  7,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
         0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0],
       [ 0,  0,  0,  0,  0,  8,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
         0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0],
       [ 0,  0,  0,  0,  0,  0,  2,  0,  0,  0,  0,  0,  0,  0,  0,  0,
         0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0],
       [ 0,  0,  0,  0,  0,  0,  0,  9,  0,  0,  0,  0,  0,  0,  0,  0,
         0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0],
       [ 0,  0,  0,  0,  0,  0,  0,  0,  7,  0,  0,  0,  0,  0,  0,  0,
         0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0],
       [ 0,  0,  0,  0,  0,  0,  0,  0,  0,  5,  0,  0,  0,  0,  0,  0,
         0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0],
       [ 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  8,  0,  0,  0,  0,  0,
         0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0],
       [ 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  1,  0,  0,  0,  0,
         0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0],
       [ 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  8,  0,  0,  0,
         0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0],
       [ 0,  1,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  8,  0,  0,
         0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0],
       [ 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  5,  0,
         0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0],
       [ 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  1,  0,  0,  0,  0,  6,
         0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0],
       [ 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
         6,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0],
       [ 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  1,  0,  0,
         0,  4,  0,  0,  0,  0,  0,  0,  0,  0,  0],
       [ 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
         0,  0, 10,  0,  0,  0,  0,  0,  0,  0,  0],
       [ 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
         0,  0,  0,  7,  0,  0,  0,  0,  0,  0,  0],
       [ 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  1,  0,  0,  0,
         0,  0,  0,  0,  5,  0,  0,  0,  0,  0,  0],
       [ 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
         0,  0,  0,  0,  0,  8,  0,  0,  0,  0,  0],
       [ 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  1,  0,  0,  0,
         0,  0,  0,  0,  0,  0,  7,  0,  0,  0,  0],
       [ 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
         0,  0,  0,  0,  0,  0,  0,  5,  0,  0,  0],
       [ 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
         0,  0,  0,  0,  0,  0,  0,  0,  5,  0,  0],
       [ 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
         0,  0,  0,  0,  0,  0,  0,  0,  0,  8,  0],
       [ 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
         0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  8]], dtype=int64)

When I try to display the confusion matrix as an image it gives me this output :

seaborn approach

I wanted to label the classes of my confusion matrix so referred to the official site of scikit-learn and figured that they have a separate class named ConfusionMatrixDisplay() to help display a confusion matrix.

However, when I try to do it using the ConfusionMatrixDisplay, I try out the following code:

import numpy as np
import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay

labels = actions

fig, ax = plt.subplots(figsize=(9, 9))
ConfusionMatrixDisplay.from_predictions(
            y_true, 
            y_pred, 
            display_labels = labels, 
            xticks_rotation=45,
            ax = ax, 
            colorbar = False)

But this time it is showing a different type of confusion matrix:

image showing second approach

Notice the values are not diagonal but scattered. How can I get the same result as the first image (using seaborn) for this approach using ConfusionMatrixDisplay?

How can I put the X-axis labels on the top for the ConfusionMatrixDisplay approach? How can I edit the "True Label" and the "Predicted label" in this case?

I also want to know how I can put the X-axis labels on the top for the seaborn approach and rotate them in 45 degrees so that they do not look overlapped.

raiyan22
  • 1,043
  • 10
  • 20
  • See also [Incorrect plot of sklearn's confusion matrix using seaborn](https://stackoverflow.com/questions/75129900/incorrect-plot-of-sklearns-confusion-matrix-using-seaborn). You could try `sns.heatmap(cm, xticklabels=actions, yticklabels=actions)`. Note that questions without reproducible test data are hard to answer. – JohanC Jan 16 '23 at 12:40

0 Answers0