Search code examples
pythonmachine-learningdeep-learningconfusion-matrix

How to get to know the order of actual labels for Confusion Matrix?


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?


Solution

  • 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.