diff --git a/Book4_Ch01_向量运算__数学要素__从加减乘除到机器学习.pdf b/Book4_Ch01_向量运算__数学要素__从加减乘除到机器学习.pdf new file mode 100644 index 0000000..2c9505e Binary files /dev/null and b/Book4_Ch01_向量运算__数学要素__从加减乘除到机器学习.pdf differ diff --git a/Book4_Ch02_Python_Codes/Bk4_Ch2_01.py b/Book4_Ch02_Python_Codes/Bk4_Ch2_01.py new file mode 100644 index 0000000..1521a46 --- /dev/null +++ b/Book4_Ch02_Python_Codes/Bk4_Ch2_01.py @@ -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 diff --git a/Book4_Ch02_Python_Codes/Bk4_Ch2_02.py b/Book4_Ch02_Python_Codes/Bk4_Ch2_02.py new file mode 100644 index 0000000..49e603a --- /dev/null +++ b/Book4_Ch02_Python_Codes/Bk4_Ch2_02.py @@ -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 diff --git a/Book4_Ch02_Python_Codes/Bk4_Ch2_03.py b/Book4_Ch02_Python_Codes/Bk4_Ch2_03.py new file mode 100644 index 0000000..2a236f1 --- /dev/null +++ b/Book4_Ch02_Python_Codes/Bk4_Ch2_03.py @@ -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 diff --git a/Book4_Ch02_Python_Codes/Bk4_Ch2_04.py b/Book4_Ch02_Python_Codes/Bk4_Ch2_04.py new file mode 100644 index 0000000..a014a3f --- /dev/null +++ b/Book4_Ch02_Python_Codes/Bk4_Ch2_04.py @@ -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) diff --git a/Book4_Ch02_Python_Codes/Bk4_Ch2_05.py b/Book4_Ch02_Python_Codes/Bk4_Ch2_05.py new file mode 100644 index 0000000..bb9d90f --- /dev/null +++ b/Book4_Ch02_Python_Codes/Bk4_Ch2_05.py @@ -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) diff --git a/Book4_Ch02_Python_Codes/Bk4_Ch2_06.py b/Book4_Ch02_Python_Codes/Bk4_Ch2_06.py new file mode 100644 index 0000000..df06a74 --- /dev/null +++ b/Book4_Ch02_Python_Codes/Bk4_Ch2_06.py @@ -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) diff --git a/Book4_Ch02_Python_Codes/Bk4_Ch2_07.py b/Book4_Ch02_Python_Codes/Bk4_Ch2_07.py new file mode 100644 index 0000000..4933a1f --- /dev/null +++ b/Book4_Ch02_Python_Codes/Bk4_Ch2_07.py @@ -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 diff --git a/Book4_Ch02_Python_Codes/Bk4_Ch2_08.py b/Book4_Ch02_Python_Codes/Bk4_Ch2_08.py new file mode 100644 index 0000000..f770058 --- /dev/null +++ b/Book4_Ch02_Python_Codes/Bk4_Ch2_08.py @@ -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')