Hinton Diagram
結合行列の視覚化を行います。ここでは、neuronの結合を想定しています。シミュレーションでは、1~Mがexcitatory neuronで、M~Nがinhibitory neuronとすると、左のほうにexcitatoryの結合が来て、右のほうにinhibitoryの結合が来るので見やすいです。
横軸がpre
で縦軸がpost
です。
メイン
以下の関数ではexcitatoryの結合を赤で、inhibitoryの結合を青で書いています。
python
def hinton(matrix, max_weight=None, ax=None):
"""結合行列を描画"""
ax = ax if ax is not None else plt.gca()
if not max_weight:
max_weight = 2 ** np.ceil(np.log(np.abs(matrix).max()) / np.log(2))
N = len(matrix)
ax.patch.set_facecolor('gray')
ax.set_aspect('equal', 'box')
ax.set_xticks(np.arange(1, N+1))
ax.set_yticks(np.arange(1, N+1))
ax.set_xlim([0.5, N+0.5])
ax.set_ylim([0.5, N+0.5])
for (x, y), w in np.ndenumerate(matrix):
color = 'red' if w > 0 else 'blue'
size = np.sqrt(np.abs(w) / max_weight)
rect = plt.Rectangle([x+1 - size / 2, y+1 - size / 2], size, size,
facecolor=color, edgecolor=color)
ax.add_patch(rect)
ax.autoscale_view()
ax.invert_yaxis()
実際に描画するときはplt.style.use("ggplot")
でいい感じになるかと思います。
以下のpsp
はnd.arrayでpsp[i][j]
はj -> i
の結合に対応します。
plot
plt.style.use("ggplot")
plt.figure(figsize=(6,6))
hinton(psp.T)
plt.xlabel("pre")
plt.ylabel("post")
plt.savefig("hinton_diagram.pdf", bbox_inches="tight", pad_inches=0)
例
適当に乱数で結合行列を生成してhinton diagramで描画した結果です。結合の強さが四角の大きさを見るだけでわかります。
参考
以下のコードを元に作成しました。
https://matplotlib.org/gallery/specialty_plots/hinton_demo.html