2

I'm confused that how do I come to know the actual labels in Confusion Matrix? I know to pass the labels but my main question is how we come to know I which sequence I've to pass the label?

from sklearn.metrics import confusion_matrix

cm = confusion_matrix(y_test,y_pred_classes)

This returns the result of the confusion_matrix() function:

Result of confusion_matrix() function

Then I declared labels and pass the labels to plot the confusion matrix:

import itertools

def plotConfusionMatrix(cm, classes, normalize=False, title='Confusion Matrix', cmap = plt.cm.Blues):

    plt.figure(figsize = (10,7))
    plt.imshow(cm, interpolation='nearest', cmap=cmap)
    plt.title(title)
    plt.colorbar()
    tick_marks = np.arange(len(classes))
    plt.xticks(tick_marks, classes, rotation=45)
    plt.yticks(tick_marks, classes)

    if normalize:
        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
        print('Normalized Confusion Matrix')
    else:
        print('Un-normalized Confusion Matrix')

    print(cm)

    thresh = cm.max()/2

    for i,j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
        plt.text(j,i, cm[i,j], horizontalalignment='center', color='white' if cm[i,j] > thresh else 'black', fontsize=25, fontweight='bold')
        plt.tight_layout()
        plt.ylabel('Actual Class')
        plt.xlabel('Predicted Class')

Then called the function and passed the labels:

classes = ['climbingdown','climbingup','jumping','lying','running','sitting','standing','walking']
plotConfusionMatrix(cm, classes)

The output for the plotted confusion matrix was:

Plotted Confusion Matrix

Now, my exact question is, I've passed the labels of each class but how will I will come to know the order in which I've to pass?

MatejMecka
  • 1,448
  • 2
  • 24
  • 37
Asif Mehmood
  • 73
  • 1
  • 8
  • Does this answer your question? [sklearn plot confusion matrix with labels](https://stackoverflow.com/questions/19233771/sklearn-plot-confusion-matrix-with-labels), in particular, check out https://stackoverflow.com/a/48018785/4240413 – Davide Fiocco Feb 02 '20 at 01:25

1 Answers1

2

You can pass the class labels into the confusion matrix function.

If you don't do that it will just use the sorted order of the labels. So i guess it depends on how your y_true and y_pred labels are mapped.

jawsem
  • 751
  • 5
  • 8