2017-06-10 68 views
1

我有一個混淆矩陣,我想繪製出來而不是僅僅打印,而我從here獲取了代碼。標籤不打印在matplotlib中圖

這是我花了一些非常微小的修改功能。

def plot_confusion_matrix(cm, classes, 
          normalize=False, 
          title='Confusion matrix', 
          cmap=plt.cm.Blues): 
    """ 
    This function prints and plots the confusion matrix. 
    Normalization can be applied by setting `normalize=True`. 
    """ 
    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('Confusion matrix, without normalization') 

    print(cm) 

    thresh = cm.max()/2. 
    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])): 
     plt.text(j, i, round(cm[i, j],4)*100, 
       horizontalalignment="center", 
       color="white" if cm[i, j] > thresh else "black") 

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

當我嘗試繪圖使用plot_confusion_matrix(cm, classes=['Front', 'Left', 'Rear', 'Right'],normalize=True,title='Confusion matrix, without normalization')

我得到這個數字 Image1

正如你所看到的,對於預測和左正確標籤缺少值的身影。當我沒有規範化地嘗試這個時,一切似乎都在起作用,我可以看到實際預測的數字與真實的標籤。

我正在使用python 3.5.3並運行Jupyter Notebook 5.0.0上的代碼。什麼可能導致這個問題?

EDIT

的釐米(混淆矩陣)之前的函數調用是

cm = np.array([[20633, 219, 357, 118], 
     [ 136, 340, 199,  0], 
     [ 49, 10, 15536, 67], 
     [ 270,  2, 196, 353]]) 
plot_confusion_matrix(cm, classes=['Front', 'Left', 'Rear', 'Right'],normalize=True,title='Confusion matrix, without normalization') 
+1

的問題是,這是不可複製的。任何人都不知道爲什麼在沒有[mcve]問題的情況下丟失了這個值? – ImportanceOfBeingErnest

+1

也許左/左組合設置爲NaN?正如@ImportanceOfBeingErnest所說,如果沒有用參數(數據,參數)實際調用函數,它不可能知道爲什麼它有這樣的錯誤。 – jlandercy

+0

編輯該問題以反映評論。我希望現在沒問題 –

回答

1

標籤是實際上有。它在幾乎白色的背景上只是白色的,所以很難看清楚。我改變了伽馬原始圖像的一點,以使其可見:

enter image description here

您可能需要改變閾值使文字白色爲更大的數字。例如。

thresh = cm.max()/1.4 

結果圖像中的預期:

enter image description here