混淆矩阵热力图如下所示:
代码如下:
import seaborn as sn #画图模块
from sklearn.metrics import confusion_matrix
def plot_matrix(y_true, y_pred,title_name):
cm = confusion_matrix(y_true, y_pred)#混淆矩阵
#annot = True 格上显示数字 ,fmt:显示数字的格式控制
ax = sn.heatmap(cm,annot=True,fmt='g',xticklabels=['1', '2', '3'],yticklabels=['1', '2', '3'])
#xticklabels、yticklabels指定横纵轴标签
ax.set_title(title_name) #标题
ax.set_xlabel('predict') #x轴
ax.set_ylabel('true') #y轴
#调用函数画图
plot=plot_matrix(T_test,adb_pre,'example-confusion matrix')
#T_test是真实值:[2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]
#adb_pre预测值:[3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]