from pylab import *
import numpy as np
import matplotlib.pyplot as plt
classes = ['猫','猴子','狗','兔子']
confusion_matrix = np.array([(666,7,43,18),(6,822,0,35),(24,1,668,1),(7,1,1,339)],dtype=np.float64)
proportion=[]
for i in confusion_matrix:
for j in i:
temp=j/(np.sum(i))
proportion.append(temp)
pshow=[]
for i in proportion:
pt="%.2f%%" % (i * 100)
pshow.append(pt)
proportion=np.array(proportion).reshape(4,4)
pshow=np.array(pshow).reshape(4,4)
config = {
"font.family":'SimHei',
}
rcParams.update(config)
plt.imshow(proportion, interpolation='nearest', cmap=plt.cm.Blues)
plt.title('confusion_matrix')
plt.colorbar()
tick_marks = np.arange(len(classes))
plt.xticks(tick_marks, classes,fontsize=14)
plt.yticks(tick_marks, classes,fontsize=14)
thresh = confusion_matrix.max() / 2.
iters = np.reshape([[[i,j] for j in range(4)] for i in range(4)],(confusion_matrix.size,2))
for i, j in iters:
if(i==j):
plt.text(j, i, format(int(confusion_matrix[i, j])), va='center', ha='center', fontsize=12,color='white',weight=5)
else:
plt.text(j, i, format(int(confusion_matrix[i, j])),va='center',ha='center',fontsize=12)
plt.ylabel('预测值',fontsize=16)
plt.xlabel('真实值',fontsize=16)
plt.tight_layout()
plt.savefig(r'C:\Users\pc\Desktop\confusion_matrix')
plt.show()