2017-12-11 9 views
0

私はあなたが以下に示す混乱マトリクスをプロットしました。 ラベルを変更したいと思います。 s1 - > s37の代わりに。私は欲しい:s1 - > s21、I1 - > I16混乱行列に2種類のティックラベルを入れるにはどうすればいいですか?

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

んが、そのことがありません。そのxticklabelsとyticklabelsで。必要なラベルを付けてカスタムリストを渡します。 –

答えて

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

関連する問題