1. 程式人生 > 其它 >混淆矩陣圖

混淆矩陣圖

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