forked from Visualize-ML/Book4_Power-of-Matrix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bk4_Ch2_13.py
40 lines (28 loc) · 922 Bytes
/
Bk4_Ch2_13.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
###############
# Authored by Weisheng Jiang
# Book 4 | From Basic Arithmetic to Machine Learning
# Published and copyrighted by Tsinghua University Press
# Beijing, China, 2022
###############
# Bk4_Ch2_13.py
import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
def plot_heatmap(x,title):
fig, ax = plt.subplots()
ax = sns.heatmap(x,
cmap='RdYlBu_r',
cbar_kws={"orientation": "horizontal"}, vmin=-1, vmax=1)
ax.set_aspect("equal")
plt.title(title)
a = np.array([[0.5],[-0.7],[1],[0.25],[-0.6],[-1]])
b = np.array([[-0.8],[0.5],[-0.6],[0.9]])
a_outer_b = np.outer(a, b)
a_outer_a = np.outer(a, a)
b_outer_b = np.outer(b, b)
# Visualizations
plot_heatmap(a,'a')
plot_heatmap(b,'b')
plot_heatmap(a_outer_b,'a outer b')
plot_heatmap(a_outer_a,'a outer a')
plot_heatmap(b_outer_b,'b outer b')