1.求取预测的矩阵
import numpy as np
from sklearn.metric import confusion_matrix
from keras import model
from keras.models import predict
c=np.zeros(shape=(9,9)) #建立一个空矩阵,一共分为9类
for i in range (10):
#取模型进行预测
a = eval("model_"+"{}".format(i+1)).predict(eval("normalization_test_"+"{}".format(i)),batch_size=1)
true_label=np.argmax(eval("y"+"{}".format(i)+"_test"),axis=1)
pre_label=np.argmax(a,axis=1)
lmr_matrix=confusion_matrix(true_label,pre_label)
c=c+lmr_matrix
2 分类的名称
classes=['wave','drink from a bottle','answer phone','clap','tight lace','sit down','stand up','read watch','bow']
3.画图
def paintConfusion_float(lmr_matrix,classes):
plt.figure(figsize=(15,10))
plt.imshow(lmr_matrix,interpolation='nearest',cmap=plt.cm.Blues)
#plt.title('confusion matrix')
#plt.colorbar()
tick_marks=np.arange(len(classes))
plt.xticks(tick_marks,classes,rotation=90,size=18)
plt.yticks(tick_marks,classes,size=18)
#plt.xlabel('Pre label',size=20)
#plt.ylabel('True label',size=20)
lmr_matrix=lmr_matrix.astype('float')/lmr_matrix.sum(axis=1)[:,np.newaxis]
fmt='.2f'
thresh=lmr_matrix.max()/2.
for i,j in itertools.product(range(lmr_matrix.shape[0]),range(lmr_matrix.shape[1])):
plt.text(j, i, format(lmr_matrix[i, j], fmt),
horizontalalignment="center",
color="black" if lmr_matrix[i, j] > thresh else "red",size=22)
plt.tight_layout()
#plt.grid()
plt.savefig("./sace_image.jpg")
plt.show()
paintConfusion_float(c,classes)