溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

怎么在python中使用confusion_matrix繪制一個(gè)混淆矩陣

發(fā)布時(shí)間:2021-03-24 16:37:21 來(lái)源:億速云 閱讀:263 作者:Leah 欄目:開(kāi)發(fā)技術(shù)

今天就跟大家聊聊有關(guān)怎么在python中使用confusion_matrix繪制一個(gè)混淆矩陣,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

Summary

涉及到分類(lèi)問(wèn)題,我們經(jīng)常需要通過(guò)可視化混淆矩陣來(lái)分析實(shí)驗(yàn)結(jié)果進(jìn)而得出調(diào)參思路,本文介紹如何利用python繪制混淆矩陣(confusion_matrix),本文只提供代碼,給出必要注釋。

Code

# -*-coding:utf-8-*-
from sklearn.metrics import confusion_matrix
import matplotlib.pyplot as plt
import numpy as np

#labels表示你不同類(lèi)別的代號(hào),比如這里的demo中有13個(gè)類(lèi)別
labels = ['A', 'B', 'C', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O']


'''
具體解釋一下re_label.txt和pr_label.txt這兩個(gè)文件,比如你有100個(gè)樣本
去做預(yù)測(cè),這100個(gè)樣本中一共有10類(lèi),那么首先這100個(gè)樣本的真實(shí)label你一定
是知道的,一共有10個(gè)類(lèi)別,用[0,9]表示,則re_label.txt文件中應(yīng)該有100
個(gè)數(shù)字,第n個(gè)數(shù)字代表的是第n個(gè)樣本的真實(shí)label(100個(gè)樣本自然就有100個(gè)
數(shù)字)。
同理,pr_label.txt里面也應(yīng)該有1--個(gè)數(shù)字,第n個(gè)數(shù)字代表的是第n個(gè)樣本經(jīng)過(guò)
你訓(xùn)練好的網(wǎng)絡(luò)預(yù)測(cè)出來(lái)的預(yù)測(cè)label。
這樣,re_label.txt和pr_label.txt這兩個(gè)文件分別代表了你樣本的真實(shí)label和預(yù)測(cè)label,然后讀到y(tǒng)_true和y_pred這兩個(gè)變量中計(jì)算后面的混淆矩陣。當(dāng)然,不一定非要使用這種txt格式的文件讀入的方式,只要你最后將你的真實(shí)
label和預(yù)測(cè)label分別保存到y(tǒng)_true和y_pred這兩個(gè)變量中即可。
'''
y_true = np.loadtxt('../Data/re_label.txt')
y_pred = np.loadtxt('../Data/pr_label.txt')

tick_marks = np.array(range(len(labels))) + 0.5

def plot_confusion_matrix(cm, title='Confusion Matrix', cmap=plt.cm.binary):
  plt.imshow(cm, interpolation='nearest', cmap=cmap)
  plt.title(title)
  plt.colorbar()
  xlocations = np.array(range(len(labels)))
  plt.xticks(xlocations, labels, rotation=90)
  plt.yticks(xlocations, labels)
  plt.ylabel('True label')
  plt.xlabel('Predicted label')
  cm = confusion_matrix(y_true, y_pred)
  np.set_printoptions(precision=2)
  
cm_normalized = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
print cm_normalized
plt.figure(figsize=(12, 8), dpi=120)

ind_array = np.arange(len(labels))
x, y = np.meshgrid(ind_array, ind_array)

for x_val, y_val in zip(x.flatten(), y.flatten()):
  c = cm_normalized[y_val][x_val]
  if c > 0.01:
    plt.text(x_val, y_val, "%0.2f" % (c,), color='red', fontsize=7, va='center', ha='center')
# offset the tick
plt.gca().set_xticks(tick_marks, minor=True)
plt.gca().set_yticks(tick_marks, minor=True)
plt.gca().xaxis.set_ticks_position('none')
plt.gca().yaxis.set_ticks_position('none')
plt.grid(True, which='minor', linestyle='-')
plt.gcf().subplots_adjust(bottom=0.15)

plot_confusion_matrix(cm_normalized, title='Normalized confusion matrix')
# show confusion matrix
plt.savefig('../Data/confusion_matrix.png', format='png')
plt.show()

Result

怎么在python中使用confusion_matrix繪制一個(gè)混淆矩陣

看完上述內(nèi)容,你們對(duì)怎么在python中使用confusion_matrix繪制一個(gè)混淆矩陣有進(jìn)一步的了解嗎?如果還想了解更多知識(shí)或者相關(guān)內(nèi)容,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI