matplotlibを使用して混同行列を描画しています:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
conf_arr_hs = [[90, 74],
[33, 131]]
norm_conf_hs = []
for i in conf_arr_hs:
a = 0
tmp_arr = []
a = sum(i, 0)
for j in i:
tmp_arr.append(float(j)/float(a))
norm_conf_hs.append(tmp_arr)
confmatmap=cm.binary
fig = plt.figure()
plt.clf()
ax = fig.add_subplot(111)
res = ax.imshow(np.array(norm_conf_hs), cmap=confmatmap, interpolation='nearest')
for x in xrange(2):
for y in xrange(2):
textcolor = 'black'
if norm_conf_hs[x][y] > 0.5:
textcolor = 'white'
ax.annotate("%0.2f"%norm_conf_hs[x][y], xy=(y, x), horizontalalignment='center', verticalalignment='center', color=textcolor)]
しかし、matplotlibは色の変更範囲を自動サイズ変更しているようです。対応する値が0.0ではなく0.2であるため、左下のグリッドは薄い灰色になっているはずです。同様に、右下のグリッドは1ではなく0.8であるため、濃い灰色になります。
カラーマッピングのダイナミックレンジを指定するステップを見逃していると思います。matplotlibのドキュメントを調べましたが、必要なものが見つかりませんでした。
カラーマップの範囲を明示的に設定するには、set_clim
次のコマンドを使用します。
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
plt.ion()
conf_arr_hs = [[90, 74],
[33, 131]]
norm_conf_hs = []
for i in conf_arr_hs:
a = 0
tmp_arr = []
a = sum(i, 0)
for j in i:
tmp_arr.append(float(j)/float(a))
norm_conf_hs.append(tmp_arr)
confmatmap=plt.cm.binary
fig = plt.figure()
plt.clf()
ax = fig.add_subplot(111)
res = ax.imshow(np.array(norm_conf_hs), cmap=confmatmap, interpolation='nearest')
res.set_clim(0,1) # set the limits for your color map
for x in xrange(2):
for y in xrange(2):
textcolor = 'black'
if norm_conf_hs[x][y] > 0.5:
textcolor = 'white'
ax.annotate("%0.2f"%norm_conf_hs[x][y], xy=(y, x), horizontalalignment='center', verticalalignment='center', color=textcolor)
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加