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,0,0,0,0,0,0,0,0,2,0,0,0,2,0,0,0,2,0,0,0,0,1,0,0,0,0,0,3,0,0,0,0,1,0,0,0,1,0,7,9,1,],
[1,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,2,0,0,1,0,3,0,5,0,1,0,1,11,0,],
[0,0,0,1,0,0,0,0,0,0,0,1,2,0,0,2,0,0,2,0,0,0,0,0,0,1,0,0,0,1,1,1,0,0,0,0,3,3,0,0,0,0,2,0,2,0,1,0,3,1,3,],
];
norm_conf = []
for i in conf_arr:
    a = 0
    tmp_arr = []
    a = sum(i, 0)
    for j in i:
        tmp_arr.append(float(j)/float(a))
    norm_conf.append(tmp_arr)
fig = plt.figure()
plt.clf()
ax = fig.add_subplot(111)
ax.set_aspect(1)
res = ax.imshow(np.array(norm_conf), cmap=plt.cm.jet,
                interpolation='nearest')
width = len(conf_arr)
height = len(conf_arr[0])
##for x in xrange(width):
##    for y in xrange(height):
##        ax.annotate(str(conf_arr[x][y]), xy=(y, x),
##                    horizontalalignment='center',
##                    verticalalignment='center')
cb = fig.colorbar(res)
alphabet = ['brush_hair','cartwheel','catch','chew','clap','climb','climb_stairs','dive','draw_sword','dribble','drink','eat','fall_floor','fencing',
'flic_flac','golf','handstand','hit','hug','jump','kick','kick_ball','kiss','laugh','pick','pour','pullup','punch','push','pushup','ride_bike',
'ride_horse','run','shake_hands','shoot_ball','shoot_bow','shoot_gun','sit','situp','smile','smoke','somersault','stand','swing_baseball','sword',
'sword_exercise','talk','throw','turn','walk','wave'];
plt.xticks(fontsize=7)
plt.yticks(fontsize=7)
locs, labels = plt.xticks(range(width), alphabet[:width])
for t in labels:
     t.set_rotation(90)
#plt.xticks('orientation', 'vertical')
#locs, labels = xticks([1,2,3,4], ['Frogs', 'Hogs', 'Bogs', 'Slogs'])
#setp(alphabet, 'rotation', 'vertical')
plt.yticks(range(height), alphabet[:height])
plt.savefig('confusion_matrix.png', format='png')


from http://blog.sina.com.cn/s/blog_4db1685d010189qj.html

你可能感兴趣的:(Python 绘制混淆矩阵(Confusion Matrix))