matplotlib畫混淆矩陣與正確率曲線的實例代碼
發(fā)布日期:2022-03-28 12:22 | 文章來源:站長之家
混淆矩陣
混淆矩陣(Confusion Matrix)是機器學習中用來總結(jié)分類模型預測結(jié)果的一個分析表,是模式識別領域中的一種常用的表達形式。它以矩陣的形式描繪樣本數(shù)據(jù)的真實屬性和分類預測結(jié)果類型之間的關(guān)系,是用來評價分類器性能的一種常用方法。
我們可以通過一個簡單的例子來直觀理解混淆矩陣
#!/usr/bin/python3.5
# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['FangSong'] #可顯示中文字符
plt.rcParams['axes.unicode_minus']=False
classes = ['a','b','c','d','e','f','g']
confusion_matrix = np.array([(99,1,2,2,0,0,6),(1,98,7,6,2,1,1),(0,0,86,0,0,2,0),(0,0,0,86,1,0,0),(0,0,0,1,94,1,0),(0,1,5,1,0,96,8),(0,0,0,4,3,0,85)],dtype=np.float64)
plt.imshow(confusion_matrix, interpolation='nearest', cmap=plt.cm.Oranges) #按照像素顯示出矩陣
plt.title('混淆矩陣')
plt.colorbar()
tick_marks = np.arange(len(classes))
plt.xticks(tick_marks, classes, rotation=-45)
plt.yticks(tick_marks, classes)
thresh = confusion_matrix.max() / 2.
#iters = [[i,j] for i in range(len(classes)) for j in range((classes))]
#ij配對,遍歷矩陣迭代器
iters = np.reshape([[[i,j] for j in range(7)] for i in range(7)],(confusion_matrix.size,2))
for i, j in iters:
plt.text(j, i, format(confusion_matrix[i, j]),fontsize=7)#顯示對應的數(shù)字
plt.ylabel('真實類別')
plt.xlabel('預測類別')
plt.tight_layout()
plt.show()
正確率曲線
fig ,ax= plt.subplots()
plt.plot(np.arange(iterations), fig_acc,'b')
plt.plot(np.arange(iterations), fig_realacc, 'r')
ax.set_xlabel('迭代次數(shù)')
ax.set_ylabel('正確率(%)')
labels = ["訓練正確率", "測試正確率"]
# labels = [l.get_label() for l in lns]
plt.legend( labels, loc=7)
plt.show()
總結(jié)
到此這篇關(guān)于matplotlib畫混淆矩陣與正確率曲線的文章就介紹到這了,更多相關(guān)matplotlib畫混淆矩陣內(nèi)容請搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來源標注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務器上建立鏡像,否則將依法追究法律責任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學習參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。
相關(guān)文章
關(guān)注官方微信