如何在混淆矩阵中有2个不同的ticklabel?

时间:2017-12-11 22:13:12

标签: python scikit-learn confusion-matrix

我已经绘制了您在下面看到的混淆矩阵。 我想更改标签。 而不是s1 - > S37。我想:s1 - > s21,I1 - > I16

import itertools

def plot_confusion_matrix(cm, title='Confusion matrix RF', cmap=plt.cm.viridis):
    plt.imshow(cm, interpolation='nearest', cmap=cmap)  #Display an image on the axes
    plt.title(title)
    plt.colorbar() #the vertical bar at the right side
    #tick_marks = np.arange(len(np.unique(y_oos))) #has the length of the # of classes (array)
    tick_marks = np.arange(37) #I KNOW IT IS HERE!!!

    plt.xticks(tick_marks, rotation=90) #to Rotate the names
    ax = plt.gca()
    ax.set_xticklabels(['s'+lab for lab in (ax.get_xticks()+1).astype(str)]) # AND ALSO HERE!!!
    plt.yticks(tick_marks)
    ax.set_yticklabels(['s'+lab for lab in (ax.get_yticks()+1).astype(str)])

    plt.tight_layout()  #
    plt.ylabel('True label')
    plt.xlabel('Predicted label')

1 个答案:

答案 0 :(得分:0)

我通过了自定义列表;如上所述。

import itertools

def plot_confusion_matrix(cm, title='Confusion matrix RF', cmap=plt.cm.viridis):
    plt.imshow(cm, interpolation='nearest', cmap=cmap)  
    plt.title(title)
    plt.colorbar() #the vertical bar at the right side

    tick_marks = np.arange(37) #21 + 16 = 37

    #THIS IS THE CUSTOM LIST!
    labels = ['s1','s2','s3','s4','s5','s6','s7','s8','s9','s10','s11','s12','s13','s14','s15','s16','s17','s18','s19','s20','s21','i1','i2','i3','i4','i5','i6','i7','i8','i9','i10','i11','i12','i13','i14','i15','i16']

    plt.xticks(tick_marks, rotation=90) #to totate the names
    ax = plt.gca()

    ax.set_xticklabels(labels)
    plt.yticks(tick_marks)

    ax.set_yticklabels(labels)
    plt.tight_layout()  #
    plt.ylabel('True label')
    plt.xlabel('Predicted label')

enter image description here