Skip to content

Commit 44c623d

Browse files
kwlee2025cppclaude
andcommitted
40 / 45 : add 'Visualizing matrices' intro section (heatmap + Hinton)
Foundation viz section between Definition and Comparison, demonstrating `plt.matshow` (continuous-magnitude story) and the Hinton diagram (square area = magnitude, color = sign; zero = empty) on the existing `mat_A`. Hinton helper inlined for Colab self-containment; matches `60_linear_algebra_2/matshow.py`. Pays forward to LA2 viz issues — subsequent notebooks can use Hinton without re-introducing the convention. Closes #371 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent abbf029 commit 44c623d

1 file changed

Lines changed: 76 additions & 3 deletions

File tree

40_linear_algebra_1/45_matrix_and_vector_numpy.ipynb

Lines changed: 76 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
{
44
"cell_type": "markdown",
55
"metadata": {
6-
"id": "view-in-github",
7-
"colab_type": "text"
6+
"colab_type": "text",
7+
"id": "view-in-github"
88
},
99
"source": [
1010
"<a href=\"https://colab.research.google.com/github/kangwonlee/nmisp/blob/main/40_linear_algebra_1/45_matrix_and_vector_numpy.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>\n"
@@ -88,6 +88,79 @@
8888
"\n"
8989
]
9090
},
91+
{
92+
"cell_type": "markdown",
93+
"metadata": {},
94+
"source": [
95+
"## 행렬 시각화<br>Visualizing matrices\n"
96+
]
97+
},
98+
{
99+
"cell_type": "markdown",
100+
"metadata": {},
101+
"source": [
102+
"행렬을 그림으로 살펴보는 두 가지 방법이 있다.<br>\n",
103+
"There are two complementary ways to visualize a matrix.\n",
104+
"\n",
105+
"- **히트맵 (Heatmap)** : 각 칸의 값을 색으로 나타낸다. 회전 행렬과 같이 값이 연속적으로 변하는 행렬에 적합하다.<br>\n",
106+
" **Heatmap** (`plt.matshow`) — colors each cell by its value. Best for matrices whose entries vary smoothly (rotations, geometric transforms).\n",
107+
"\n",
108+
"- **힌튼 도식 (Hinton diagram)** : 각 칸에 정사각형을 그리되, **정사각형의 넓이는 값의 크기에 비례**하고 **색은 부호를 나타낸다** (흰색 = 양수, 검은색 = 음수). 값이 0인 칸은 *문자 그대로 비어 있게* 보이므로, 행렬의 **구조나 희소성 (sparsity)** 이 한눈에 들어온다.<br>\n",
109+
" **Hinton diagram** — draws a square in each cell whose **area is proportional to the magnitude** and whose **color shows the sign** (white = positive, black = negative). Zero entries are *literally empty*, so structure and sparsity become immediately legible.\n",
110+
"\n",
111+
"아래에서 같은 행렬 `mat_A` 를 두 방법으로 그려 비교해 본다.<br>\n",
112+
"Below, the same `mat_A` is drawn both ways for comparison.\n"
113+
]
114+
},
115+
{
116+
"cell_type": "code",
117+
"execution_count": null,
118+
"metadata": {},
119+
"outputs": [],
120+
"source": [
121+
"# Hinton diagram helper\n",
122+
"# (also available at 60_linear_algebra_2/matshow.py)\n",
123+
"# Reference: matplotlib gallery — hinton_demo.html\n",
124+
"def hinton(matrix, max_weight=None, ax=None):\n",
125+
" if ax is None:\n",
126+
" ax = plt.gca()\n",
127+
" if max_weight is None:\n",
128+
" max_weight = 2 ** np.ceil(np.log2(np.abs(matrix).max()))\n",
129+
" ax.patch.set_facecolor('gray')\n",
130+
" ax.set_aspect('equal', 'box')\n",
131+
" ax.xaxis.set_major_locator(plt.NullLocator())\n",
132+
" ax.yaxis.set_major_locator(plt.NullLocator())\n",
133+
" for (y, x), w in np.ndenumerate(matrix):\n",
134+
" color = 'white' if w > 0 else 'black'\n",
135+
" size = np.sqrt(abs(w) / max_weight)\n",
136+
" rect = plt.Rectangle(\n",
137+
" [x - size / 2, y - size / 2], size, size,\n",
138+
" facecolor=color, edgecolor=color,\n",
139+
" )\n",
140+
" ax.add_patch(rect)\n",
141+
" ax.autoscale_view()\n",
142+
" ax.invert_yaxis()\n"
143+
]
144+
},
145+
{
146+
"cell_type": "code",
147+
"execution_count": null,
148+
"metadata": {},
149+
"outputs": [],
150+
"source": [
151+
"fig, (ax_heat, ax_hint) = plt.subplots(1, 2, figsize=(10, 4))\n",
152+
"\n",
153+
"im = ax_heat.matshow(mat_A)\n",
154+
"plt.colorbar(im, ax=ax_heat, fraction=0.046, pad=0.04)\n",
155+
"ax_heat.set_title('Heatmap')\n",
156+
"\n",
157+
"hinton(mat_A, ax=ax_hint)\n",
158+
"ax_hint.set_title('Hinton diagram')\n",
159+
"\n",
160+
"plt.tight_layout()\n",
161+
"plt.show()\n"
162+
]
163+
},
91164
{
92165
"cell_type": "markdown",
93166
"metadata": {},
@@ -570,4 +643,4 @@
570643
},
571644
"nbformat": 4,
572645
"nbformat_minor": 4
573-
}
646+
}

0 commit comments

Comments
 (0)