mirror of
https://github.com/Visualize-ML/Book4_Power-of-Matrix.git
synced 2026-05-05 13:53:46 +08:00
Add files via upload
This commit is contained in:
20
Book4_Ch02_Python_Codes/Bk4_Ch2_01.py
Normal file
20
Book4_Ch02_Python_Codes/Bk4_Ch2_01.py
Normal file
@@ -0,0 +1,20 @@
|
||||
|
||||
###############
|
||||
# Authored by Weisheng Jiang
|
||||
# Book 4 | From Basic Arithmetic to Machine Learning
|
||||
# Published and copyrighted by Tsinghua University Press
|
||||
# Beijing, China, 2022
|
||||
###############
|
||||
|
||||
# Bk4_Ch2_01.py
|
||||
|
||||
import numpy as np
|
||||
|
||||
a = np.array([[4, 3]])
|
||||
b = np.array([[5, -2]])
|
||||
|
||||
a_dot_b = np.inner(a, b)
|
||||
|
||||
a_2 = np.array([[4], [3]])
|
||||
b_2 = np.array([[5], [-2]])
|
||||
a_dot_b_2 = a_2.T@b_2
|
||||
19
Book4_Ch02_Python_Codes/Bk4_Ch2_02.py
Normal file
19
Book4_Ch02_Python_Codes/Bk4_Ch2_02.py
Normal file
@@ -0,0 +1,19 @@
|
||||
|
||||
###############
|
||||
# Authored by Weisheng Jiang
|
||||
# Book 4 | From Basic Arithmetic to Machine Learning
|
||||
# Published and copyrighted by Tsinghua University Press
|
||||
# Beijing, China, 2022
|
||||
###############
|
||||
|
||||
# Bk4_Ch2_02.py
|
||||
|
||||
import numpy as np
|
||||
a = np.array([[2,3],
|
||||
[3,4]])
|
||||
|
||||
b = np.array([[3,4],
|
||||
[5,6]])
|
||||
|
||||
a_@_b = np.dot(a,b)
|
||||
# a@b
|
||||
19
Book4_Ch02_Python_Codes/Bk4_Ch2_03.py
Normal file
19
Book4_Ch02_Python_Codes/Bk4_Ch2_03.py
Normal file
@@ -0,0 +1,19 @@
|
||||
|
||||
###############
|
||||
# Authored by Weisheng Jiang
|
||||
# Book 4 | From Basic Arithmetic to Machine Learning
|
||||
# Published and copyrighted by Tsinghua University Press
|
||||
# Beijing, China, 2022
|
||||
###############
|
||||
|
||||
# Bk4_Ch2_03.py
|
||||
|
||||
import numpy as np
|
||||
a = np.array([[1,2],
|
||||
[3,4]])
|
||||
|
||||
b = np.array([[3,4],
|
||||
[5,6]])
|
||||
|
||||
a_dot_b = np.vdot(a,b)
|
||||
# [1,2,3,4]*[3,4,5,6].T
|
||||
22
Book4_Ch02_Python_Codes/Bk4_Ch2_04.py
Normal file
22
Book4_Ch02_Python_Codes/Bk4_Ch2_04.py
Normal file
@@ -0,0 +1,22 @@
|
||||
|
||||
###############
|
||||
# Authored by Weisheng Jiang
|
||||
# Book 4 | From Basic Arithmetic to Machine Learning
|
||||
# Published and copyrighted by Tsinghua University Press
|
||||
# Beijing, China, 2022
|
||||
###############
|
||||
|
||||
# Bk4_Ch2_04.py
|
||||
|
||||
import numpy as np
|
||||
|
||||
a, b = np.array([[4], [3]]), np.array([[5], [-2]])
|
||||
|
||||
# calculate cosine theta
|
||||
cos_theta = (a.T @ b) / (np.linalg.norm(a,2) * np.linalg.norm(b,2))
|
||||
|
||||
# calculate theta in radian
|
||||
cos_radian = np.arccos(cos_theta)
|
||||
|
||||
# convert radian to degree
|
||||
cos_degree = cos_radian * ((180)/np.pi)
|
||||
37
Book4_Ch02_Python_Codes/Bk4_Ch2_05.py
Normal file
37
Book4_Ch02_Python_Codes/Bk4_Ch2_05.py
Normal file
@@ -0,0 +1,37 @@
|
||||
|
||||
###############
|
||||
# Authored by Weisheng Jiang
|
||||
# Book 4 | From Basic Arithmetic to Machine Learning
|
||||
# Published and copyrighted by Tsinghua University Press
|
||||
# Beijing, China, 2022
|
||||
###############
|
||||
|
||||
# Bk4_Ch2_05.py
|
||||
|
||||
from scipy.spatial import distance
|
||||
from sklearn import datasets
|
||||
import numpy as np
|
||||
|
||||
# import the iris data
|
||||
iris = datasets.load_iris()
|
||||
|
||||
# Only use the first two features: sepal length, sepal width
|
||||
X = iris.data[:, :]
|
||||
|
||||
# Extract 4 data points
|
||||
x1_data = X[0,:]
|
||||
x2_data = X[1,:]
|
||||
x51_data = X[50,:]
|
||||
x101_data = X[100,:]
|
||||
|
||||
# calculate cosine distance
|
||||
x1_x2_cos_dist = distance.cosine(x1_data,x2_data)
|
||||
x1_norm = np.linalg.norm(x1_data)
|
||||
x2_norm = np.linalg.norm(x2_data)
|
||||
x1_dot_x2 = x1_data.T@x2_data
|
||||
x1_x2_cos = x1_dot_x2/x1_norm/x2_norm
|
||||
|
||||
|
||||
x1_x51_cos_dist = distance.cosine(x1_data,x51_data)
|
||||
|
||||
x1_x101_cos_dist = distance.cosine(x1_data,x101_data)
|
||||
24
Book4_Ch02_Python_Codes/Bk4_Ch2_06.py
Normal file
24
Book4_Ch02_Python_Codes/Bk4_Ch2_06.py
Normal file
@@ -0,0 +1,24 @@
|
||||
|
||||
###############
|
||||
# Authored by Weisheng Jiang
|
||||
# Book 4 | From Basic Arithmetic to Machine Learning
|
||||
# Published and copyrighted by Tsinghua University Press
|
||||
# Beijing, China, 2022
|
||||
###############
|
||||
|
||||
# Bk4_Ch2_06.py
|
||||
|
||||
import numpy as np
|
||||
a = np.array([-2, 1, 1])
|
||||
b = np.array([1, -2, -1])
|
||||
# a = [-2, 1, 1]
|
||||
# b = [1, -2, -1]
|
||||
|
||||
# calculate cross product of row vectors
|
||||
a_cross_b = np.cross(a, b)
|
||||
|
||||
a_col = np.array([[-2], [1], [1]])
|
||||
b_col = np.array([[1], [-2], [-1]])
|
||||
|
||||
# calculate cross product of column vectors
|
||||
a_cross_b_col = np.cross(a_col,b_col,axis=0)
|
||||
27
Book4_Ch02_Python_Codes/Bk4_Ch2_07.py
Normal file
27
Book4_Ch02_Python_Codes/Bk4_Ch2_07.py
Normal file
@@ -0,0 +1,27 @@
|
||||
|
||||
###############
|
||||
# Authored by Weisheng Jiang
|
||||
# Book 4 | From Basic Arithmetic to Machine Learning
|
||||
# Published and copyrighted by Tsinghua University Press
|
||||
# Beijing, China, 2022
|
||||
###############
|
||||
|
||||
# Bk4_Ch2_07.py
|
||||
|
||||
import numpy as np
|
||||
a = np.array([-2, 1, 1])
|
||||
b = np.array([1, -2, -1])
|
||||
# a = [-2, 1, 1]
|
||||
# b = [1, -2, -1]
|
||||
|
||||
|
||||
# calculate element-wise product of row vectors
|
||||
a_times_b = np.multiply(a, b)
|
||||
a_times_b_2 = a*b
|
||||
|
||||
a_col = np.array([[-2], [1], [1]])
|
||||
b_col = np.array([[1], [-2], [-1]])
|
||||
|
||||
# calculate element-wise product of column vectors
|
||||
a_times_b_col = np.multiply(a_col,b_col)
|
||||
a_times_b_col_2 = a_col*b_col
|
||||
40
Book4_Ch02_Python_Codes/Bk4_Ch2_08.py
Normal file
40
Book4_Ch02_Python_Codes/Bk4_Ch2_08.py
Normal file
@@ -0,0 +1,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_08.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')
|
||||
Reference in New Issue
Block a user