混淆矩陣圖
阿新 • • 發佈:2021-10-02
import pandas as pd import matplotlib.pyplot as plt # confusion_matrix = [[550, 55, 100], [12, 591, 23], [89, 72, 459]] confusion_matrix = {'A':{'A': 550, 'B': 55, 'C': 100}, 'B':{'A': 12, 'B': 591, 'C': 23}, 'C':{'A': 89, 'B': 72, 'C': 459}} pd_cm = pd.DataFrame(confusion_matrix).T.fillna(0) row_keys = pd_cm.index.values.tolist() col_keys = pd_cm.columns.values.tolist() fig = plt.figure() plt.clf() axes = fig.add_subplot(111) axes.set_aspect(1) res = axes.imshow(pd_cm, cmap=plt.cm.jet, interpolation='nearest') array_list = pd_cm.values.tolist() for x in range(len(row_keys)): for y in range(len(col_keys)): axes.annotate( str(array_list[x][y]), xy=(y, x), horizontalalignment='center', verticalalignment='center', fontsize=21) fig.colorbar(res, fraction=0.046, pad=0.04) plt.xticks(range(len(col_keys)), col_keys, fontsize=14, rotation=0) plt.yticks(range(len(row_keys)), row_keys, fontsize=14, rotation=0) plt.savefig("confusion_matrix.png", tight_layout=False)
原文連結:https://blog.csdn.net/u011503666/article/details/108408698