Pyhon 2.7
IDE Pycharm 5.0.3
numpy 1.11.0
目前接口庫
@MrLevo520–數(shù)據(jù)轉(zhuǎn)化接口
仍在不斷更新
目的
將混淆矩陣可視化展現(xiàn)出來
準(zhǔn)備工作
請先安裝numpy,matplotlib
接口代碼
新建一個(gè)confusion_matrix_png.py文件,輸入如下代碼
# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt
from pylab import *
# 其中cm是計(jì)算好的混淆矩陣
# cm = confusion_matrix(test_label, predict_label)
# 比如上述這樣產(chǎn)生cm
def ConfusionMatrixPng(cm,classlist):
norm_conf = []
for i in cm:
a = 0
tmp_arr = []
a = sum(i, 0)
for j in i:
tmp_arr.append(float(j) / float(a))
norm_conf.append(tmp_arr)
fig = plt.figure()
plt.clf()
ax = fig.add_subplot(111)
ax.set_aspect(1)
res = ax.imshow(np.array(norm_conf), cmap=plt.cm.jet,
interpolation='nearest')
width = len(cm)
height = len(cm[0])
cb = fig.colorbar(res)
alphabet = classlist
plt.xticks(fontsize=7)
plt.yticks(fontsize=7)
locs, labels = plt.xticks(range(width), alphabet[:width])
for t in labels:
t.set_rotation(90)
# plt.xticks('orientation', 'vertical')
# locs, labels = xticks([1,2,3,4], ['Frogs', 'Hogs', 'Bogs', 'Slogs'])
# setp(alphabet, 'rotation', 'vertical')
plt.yticks(range(height), alphabet[:height])
plt.savefig('confusion_matrix.png', format='png')
plt.show()
接口說明
ConfusionMatrixPng(cm,classlist)
# cm 是混淆矩陣,由confusion_matrix(test_label, predict_label)產(chǎn)生
# classlist是需要混淆矩陣中的各類別,也就是橫/縱坐標(biāo)組成的列表
接口(函數(shù))調(diào)用實(shí)例
在同一工程目錄下的另一個(gè)py文件中只需要操作如下
import confusion_matrix_png as cmp
cm = xxx(找不到數(shù)據(jù)了額。。)
classlist = ['4', '5', '6', '11', '15', '16', '17', '20', '22', '24', '28', '30', '32', '33','34', '35', '36', '37', '38', '40', '41', '45', '49', '50', '63', '72']
cmp.ConfusionMatrixPng(cm,classlist)
執(zhí)行效果
混淆矩陣可視化
(我現(xiàn)在處理的樣本精度很低,所以展示效果不是很好)
最后
調(diào)用接口的好處就是在一個(gè)大的項(xiàng)目中,分割開來處理,每個(gè)人寫規(guī)范的接口文件,之后想用哪個(gè)模塊直接用就可以了,當(dāng)然,文檔和接口規(guī)范得自己注意!
致謝
提供代碼的博客我找不到了,很抱歉,我是在他的基礎(chǔ)上封裝了一下函數(shù)而已,自己沒有很多工作量,所以很抱歉。