Summary
涉及到分類問題,我們經常需要通過可視化混淆矩陣來分析實驗結果進而得出調參思路,本文介紹如何利用python繪製混淆矩陣(confusion_matrix),本文只提供代碼,給出必要注釋。 Code
# -*-coding:utf-8-*-from sklearn.metrics import confusion_matriximport matplotlib.pyplot as pltimport numpy as np#labels表示你不同類別的代號,比如這裡的demo中有13個類別labels = ['A', 'B', 'C', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O']#y_true代表真實的label值 y_pred代表預測得到的lavel值y_true = np.loadtxt('../Data/re_label.txt')y_pred = np.loadtxt('../Data/pr_label.txt')tick_marks = np.array(range(len(labels))) + 0.5def plot_confusion_matrix(cm, title='Confusion Matrix', cmap=plt.cm.binary): plt.imshow(cm, interpolation='nearest', cmap=cmap) plt.title(title) plt.colorbar() xlocations = np.array(range(len(labels))) plt.xticks(xlocations, labels, rotation=90) plt.yticks(xlocations, labels) plt.ylabel('True label') plt.xlabel('Predicted label')cm = confusion_matrix(y_true, y_pred)np.set_printoptions(precision=2)cm_normalized = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]print cm_normalizedplt.figure(figsize=(12, 8), dpi=120)ind_array = np.arange(len(labels))x, y = np.meshgrid(ind_array, ind_array)for x_val, y_val in zip(x.flatten(), y.flatten()): c = cm_normalized[y_val][x_val] if c > 0.01: plt.text(x_val, y_val, "%0.2f" % (c,), color='red', fontsize=7, va='center', ha='center')# offset the tickplt.gca().set_xticks(tick_marks, minor=True)plt.gca().set_yticks(tick_marks, minor=True)plt.gca().xaxis.set_ticks_position('none')plt.gca().yaxis.set_ticks_position('none')plt.grid(True, which='minor', linestyle='-')plt.gcf().subplots_adjust(bottom=0.15)plot_confusion_matrix(cm_normalized, title='Normalized confusion matrix')# show confusion matrixplt.savefig('../Data/confusion_matrix.png', format='png')plt.show()
Result
Instructions
按照代碼中的注釋將labels、y_true 、y_pred替換為你自己的資料即可。 Reference
如何用python畫好confusion matrix