【Python】 confusion_matrtixの作り方

seabornのheatmapによって表現できる.

import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
# scikit-learnのconfusion_matrixクラスで
b = np.array([[251,  22,   1,   3,   0,   1,   3,  16,  24],
               [  8, 459,   0,   2,   0,   1,   0,   4,  23],
               [  0,   1, 557,   0,   0,   0,   0,   1,   0],
               [  0,   3,   0,  90,   0,   0,   0,   0,   0],
               [  0,   0,   0,   1,   1,   0,   0,   4,   0],
               [ 35,  20,   1,   8,   0,  50,   0,   7,  31],
               [  0,   3,   0,   5,   0,   0,  78,   0,   0],
               [ 11,   1,   4,   9,   0,   1,   0, 228,   1],
               [ 34,  39,   0,   0,   0,   2,   0,  11, 119]])
labels = ["a","b","c","d","e","f","g","h","i"]
#pandas.dataframeに格納
b = pd.DataFrame(b,index=labels,columns=index)
sns.heatmap(b,annot=True,cmap="Reds")
plt.show()