matplotlib畫混淆矩陣與正確率曲線的實例代碼
混淆矩陣
混淆矩陣(Confusion Matrix)是機器學(xué)習(xí)中用來總結(jié)分類模型預(yù)測結(jié)果的一個分析表,是模式識別領(lǐng)域中的一種常用的表達形式。它以矩陣的形式描繪樣本數(shù)據(jù)的真實屬性和分類預(yù)測結(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) #顯示對應(yīng)的數(shù)字 plt.ylabel('真實類別') plt.xlabel('預(yù)測類別') 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 = ["訓(xùn)練正確率", "測試正確率"] # 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)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python matplotlib.pyplot.plot()參數(shù)用法
這篇文章主要介紹了python matplotlib.pyplot.plot()參數(shù)用法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04Python中面向?qū)ο竽銘?yīng)該知道的一下知識
這篇文章主要介紹了Python中面向?qū)ο竽銘?yīng)該知道的一下知識,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-07-07python-for x in range的用法(注意要點、細(xì)節(jié))
這篇文章主要介紹了python-for x in range的用法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-05-05