混淆矩陣(Confusion Matrix),是一種在深度學習中經常使用的輔助工具,可讓你直觀地瞭解你的模型在哪一類樣本里面表現得不是很好。html
如上圖,咱們就能夠看到,有一個樣本本來是0的,卻被預測成了1,還有一個,本來是2的,卻被預測成了0。工具
簡單介紹做用後,下面上代碼:學習
import seaborn as sns from sklearn.metrics import confusion_matrix import matplotlib.pyplot as plt
導入須要的包,若是有一些包沒有,pip一下就能夠了。spa
sns.set() f,ax=plt.subplots() y_true = [0,0,1,2,1,2,0,2,2,0,1,1] y_pred = [1,0,1,2,1,0,0,2,2,0,1,1] C2= confusion_matrix(y_true, y_pred, labels=[0, 1, 2]) print(C2) #打印出來看看 sns.heatmap(C2,annot=True,ax=ax) #畫熱力圖 ax.set_title('confusion matrix') #標題 ax.set_xlabel('predict') #x軸 ax.set_ylabel('true') #y軸
下面就是結果:3d
原文出處:https://www.cnblogs.com/yexionglin/p/11432180.htmlcode