From 9822d0a7e52087b32779abbfe4f53d6c5601c452 Mon Sep 17 00:00:00 2001 From: Visualize-ML <105787223+Visualize-ML@users.noreply.github.com> Date: Fri, 2 Sep 2022 06:06:41 -0400 Subject: [PATCH] Add files via upload --- Book4_Ch02_Python_Codes/Bk4_Ch2_01.py | 22 +++++++++++---- Book4_Ch02_Python_Codes/Bk4_Ch2_02.py | 12 ++++---- Book4_Ch02_Python_Codes/Bk4_Ch2_03.py | 31 ++++++++++++++++----- Book4_Ch02_Python_Codes/Bk4_Ch2_04.py | 20 ++++++++------ Book4_Ch02_Python_Codes/Bk4_Ch2_05.py | 28 +++---------------- Book4_Ch02_Python_Codes/Bk4_Ch2_06.py | 16 ++++------- Book4_Ch02_Python_Codes/Bk4_Ch2_07.py | 20 ++++---------- Book4_Ch02_Python_Codes/Bk4_Ch2_08.py | 33 ++++------------------ Book4_Ch02_Python_Codes/Bk4_Ch2_09.py | 22 +++++++++++++++ Book4_Ch02_Python_Codes/Bk4_Ch2_10.py | 37 +++++++++++++++++++++++++ Book4_Ch02_Python_Codes/Bk4_Ch2_11.py | 24 ++++++++++++++++ Book4_Ch02_Python_Codes/Bk4_Ch2_12.py | 27 ++++++++++++++++++ Book4_Ch02_Python_Codes/Bk4_Ch2_13.py | 40 +++++++++++++++++++++++++++ 13 files changed, 230 insertions(+), 102 deletions(-) create mode 100644 Book4_Ch02_Python_Codes/Bk4_Ch2_09.py create mode 100644 Book4_Ch02_Python_Codes/Bk4_Ch2_10.py create mode 100644 Book4_Ch02_Python_Codes/Bk4_Ch2_11.py create mode 100644 Book4_Ch02_Python_Codes/Bk4_Ch2_12.py create mode 100644 Book4_Ch02_Python_Codes/Bk4_Ch2_13.py diff --git a/Book4_Ch02_Python_Codes/Bk4_Ch2_01.py b/Book4_Ch02_Python_Codes/Bk4_Ch2_01.py index 1521a46..1949300 100644 --- a/Book4_Ch02_Python_Codes/Bk4_Ch2_01.py +++ b/Book4_Ch02_Python_Codes/Bk4_Ch2_01.py @@ -9,12 +9,22 @@ # Bk4_Ch2_01.py import numpy as np +import matplotlib.pyplot as plt -a = np.array([[4, 3]]) -b = np.array([[5, -2]]) +def draw_vector(vector,RBG): + array = np.array([[0, 0, vector[0], vector[1]]]) + X, Y, U, V = zip(*array) + plt.quiver(X, Y, U, V,angles='xy', scale_units='xy',scale=1,color = RBG) -a_dot_b = np.inner(a, b) +fig, ax = plt.subplots() -a_2 = np.array([[4], [3]]) -b_2 = np.array([[5], [-2]]) -a_dot_b_2 = a_2.T@b_2 +draw_vector([4,3],np.array([0,112,192])/255) +draw_vector([-3,4],np.array([255,0,0])/255) + +plt.ylabel('$x_2$') +plt.xlabel('$x_1$') +plt.axis('scaled') +ax.set_xlim([-5, 5]) +ax.set_ylim([-5, 5]) +ax.grid(linestyle='--', linewidth=0.25, color=[0.5,0.5,0.5]) +plt.show() diff --git a/Book4_Ch02_Python_Codes/Bk4_Ch2_02.py b/Book4_Ch02_Python_Codes/Bk4_Ch2_02.py index 49e603a..c7aaa78 100644 --- a/Book4_Ch02_Python_Codes/Bk4_Ch2_02.py +++ b/Book4_Ch02_Python_Codes/Bk4_Ch2_02.py @@ -9,11 +9,11 @@ # Bk4_Ch2_02.py import numpy as np -a = np.array([[2,3], - [3,4]]) -b = np.array([[3,4], - [5,6]]) +# define two column vectors +a = np.array([[4], [3]]) +b = np.array([[-3], [4]]) -a_@_b = np.dot(a,b) -# a@b +# calculate L2 norm +a_L2_norm = np.linalg.norm(a) +b_L2_norm = np.linalg.norm(b) diff --git a/Book4_Ch02_Python_Codes/Bk4_Ch2_03.py b/Book4_Ch02_Python_Codes/Bk4_Ch2_03.py index 2a236f1..d16adf1 100644 --- a/Book4_Ch02_Python_Codes/Bk4_Ch2_03.py +++ b/Book4_Ch02_Python_Codes/Bk4_Ch2_03.py @@ -6,14 +6,31 @@ # Beijing, China, 2022 ############### -# Bk4_Ch2_03.py +# Bk4_Ch1_03.py +import matplotlib.pyplot as plt import numpy as np -a = np.array([[1,2], - [3,4]]) -b = np.array([[3,4], - [5,6]]) +x1 = np.linspace(-10, 10, num=201); +x2 = x1; -a_dot_b = np.vdot(a,b) -# [1,2,3,4]*[3,4,5,6].T +xx1, xx2 = np.meshgrid(x1,x2) +p = 2 +zz = ((np.abs((xx1))**p) + (np.abs((xx2))**p))**(1./p) + +fig, ax = plt.subplots(figsize=(12, 12)) + +ax.contour(xx1, xx2, zz, levels = np.arange(11), cmap='RdYlBu_r') + +ax.axhline(y=0, color='k', linewidth = 0.25) +ax.axvline(x=0, color='k', linewidth = 0.25) +ax.set_xlim(-12, 12) +ax.set_ylim(-12, 12) +ax.spines['top'].set_visible(False) +ax.spines['right'].set_visible(False) +ax.spines['bottom'].set_visible(False) +ax.spines['left'].set_visible(False) +ax.set_xlabel('$x_1$') +ax.set_ylabel('$x_2$') +ax.set_aspect('equal', adjustable='box') +plt.show() diff --git a/Book4_Ch02_Python_Codes/Bk4_Ch2_04.py b/Book4_Ch02_Python_Codes/Bk4_Ch2_04.py index a014a3f..a9e05db 100644 --- a/Book4_Ch02_Python_Codes/Bk4_Ch2_04.py +++ b/Book4_Ch02_Python_Codes/Bk4_Ch2_04.py @@ -6,17 +6,21 @@ # Beijing, China, 2022 ############### -# Bk4_Ch2_04.py +# Bk4_Ch1_04.py import numpy as np -a, b = np.array([[4], [3]]), np.array([[5], [-2]]) +# define two column vectors +a = np.array([[-2], [5]]) +b = np.array([[5], [-1]]) -# calculate cosine theta -cos_theta = (a.T @ b) / (np.linalg.norm(a,2) * np.linalg.norm(b,2)) +# calculate vector addition +a_plus_b = a + b +a_plus_b_2 = np.add(a,b) -# calculate theta in radian -cos_radian = np.arccos(cos_theta) +# calculate vector subtraction +a_minus_b = a - b +a_minus_b_2 = np.subtract(a,b) -# convert radian to degree -cos_degree = cos_radian * ((180)/np.pi) +b_minus_a = b - a +b_minus_a_2 = np.subtract(b,a) diff --git a/Book4_Ch02_Python_Codes/Bk4_Ch2_05.py b/Book4_Ch02_Python_Codes/Bk4_Ch2_05.py index bb9d90f..45ced0c 100644 --- a/Book4_Ch02_Python_Codes/Bk4_Ch2_05.py +++ b/Book4_Ch02_Python_Codes/Bk4_Ch2_05.py @@ -8,30 +8,10 @@ # 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() +# define a column vector +a = np.array([[2], [2]]) -# 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) +b = 2*a +c = -1.5*a diff --git a/Book4_Ch02_Python_Codes/Bk4_Ch2_06.py b/Book4_Ch02_Python_Codes/Bk4_Ch2_06.py index df06a74..63933d7 100644 --- a/Book4_Ch02_Python_Codes/Bk4_Ch2_06.py +++ b/Book4_Ch02_Python_Codes/Bk4_Ch2_06.py @@ -9,16 +9,12 @@ # 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 = np.array([[4, 3]]) +b = np.array([[5, -2]]) -a_col = np.array([[-2], [1], [1]]) -b_col = np.array([[1], [-2], [-1]]) +a_dot_b = np.inner(a, b) -# calculate cross product of column vectors -a_cross_b_col = np.cross(a_col,b_col,axis=0) +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_07.py b/Book4_Ch02_Python_Codes/Bk4_Ch2_07.py index 4933a1f..773c9cc 100644 --- a/Book4_Ch02_Python_Codes/Bk4_Ch2_07.py +++ b/Book4_Ch02_Python_Codes/Bk4_Ch2_07.py @@ -9,19 +9,11 @@ # 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] +a = np.array([[2,3], + [3,4]]) +b = np.array([[3,4], + [5,6]]) -# 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 +a_@_b = np.dot(a,b) +# a@b diff --git a/Book4_Ch02_Python_Codes/Bk4_Ch2_08.py b/Book4_Ch02_Python_Codes/Bk4_Ch2_08.py index f770058..2e86111 100644 --- a/Book4_Ch02_Python_Codes/Bk4_Ch2_08.py +++ b/Book4_Ch02_Python_Codes/Bk4_Ch2_08.py @@ -8,33 +8,12 @@ # Bk4_Ch2_08.py -import matplotlib.pyplot as plt import numpy as np -import seaborn as sns +a = np.array([[1,2], + [3,4]]) -def plot_heatmap(x,title): +b = np.array([[3,4], + [5,6]]) - 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') +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_09.py b/Book4_Ch02_Python_Codes/Bk4_Ch2_09.py new file mode 100644 index 0000000..7d64826 --- /dev/null +++ b/Book4_Ch02_Python_Codes/Bk4_Ch2_09.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_09.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_10.py b/Book4_Ch02_Python_Codes/Bk4_Ch2_10.py new file mode 100644 index 0000000..a247aee --- /dev/null +++ b/Book4_Ch02_Python_Codes/Bk4_Ch2_10.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_10.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_11.py b/Book4_Ch02_Python_Codes/Bk4_Ch2_11.py new file mode 100644 index 0000000..1404f15 --- /dev/null +++ b/Book4_Ch02_Python_Codes/Bk4_Ch2_11.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_11.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_12.py b/Book4_Ch02_Python_Codes/Bk4_Ch2_12.py new file mode 100644 index 0000000..d3f65d4 --- /dev/null +++ b/Book4_Ch02_Python_Codes/Bk4_Ch2_12.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_12.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_13.py b/Book4_Ch02_Python_Codes/Bk4_Ch2_13.py new file mode 100644 index 0000000..43a539a --- /dev/null +++ b/Book4_Ch02_Python_Codes/Bk4_Ch2_13.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_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')