diff --git a/Book4_Ch01_Python_Codes/Bk4_Ch1_01.py b/Book4_Ch01_Python_Codes/Bk4_Ch1_01.py new file mode 100644 index 0000000..bda5492 --- /dev/null +++ b/Book4_Ch01_Python_Codes/Bk4_Ch1_01.py @@ -0,0 +1,30 @@ + +############### +# Authored by Weisheng Jiang +# Book 4 | From Basic Arithmetic to Machine Learning +# Published and copyrighted by Tsinghua University Press +# Beijing, China, 2022 +############### + +# Bk4_Ch1_01.py + +import numpy as np +import matplotlib.pyplot as plt + +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) + +fig, ax = plt.subplots() + +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_Ch01_Python_Codes/Bk4_Ch1_02.py b/Book4_Ch01_Python_Codes/Bk4_Ch1_02.py new file mode 100644 index 0000000..8bf7e9e --- /dev/null +++ b/Book4_Ch01_Python_Codes/Bk4_Ch1_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_Ch1_02.py + +import numpy as np + +# define two column vectors +a = np.array([[4], [3]]) +b = np.array([[-3], [4]]) + +# calculate L2 norm +a_L2_norm = np.linalg.norm(a) +b_L2_norm = np.linalg.norm(b) diff --git a/Book4_Ch01_Python_Codes/Bk4_Ch1_03.py b/Book4_Ch01_Python_Codes/Bk4_Ch1_03.py new file mode 100644 index 0000000..d16adf1 --- /dev/null +++ b/Book4_Ch01_Python_Codes/Bk4_Ch1_03.py @@ -0,0 +1,36 @@ + +############### +# Authored by Weisheng Jiang +# Book 4 | From Basic Arithmetic to Machine Learning +# Published and copyrighted by Tsinghua University Press +# Beijing, China, 2022 +############### + +# Bk4_Ch1_03.py + +import matplotlib.pyplot as plt +import numpy as np + +x1 = np.linspace(-10, 10, num=201); +x2 = x1; + +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_Ch01_Python_Codes/Bk4_Ch1_04.py b/Book4_Ch01_Python_Codes/Bk4_Ch1_04.py new file mode 100644 index 0000000..a9e05db --- /dev/null +++ b/Book4_Ch01_Python_Codes/Bk4_Ch1_04.py @@ -0,0 +1,26 @@ + +############### +# Authored by Weisheng Jiang +# Book 4 | From Basic Arithmetic to Machine Learning +# Published and copyrighted by Tsinghua University Press +# Beijing, China, 2022 +############### + +# Bk4_Ch1_04.py + +import numpy as np + +# define two column vectors +a = np.array([[-2], [5]]) +b = np.array([[5], [-1]]) + +# calculate vector addition +a_plus_b = a + b +a_plus_b_2 = np.add(a,b) + +# calculate vector subtraction +a_minus_b = a - b +a_minus_b_2 = np.subtract(a,b) + +b_minus_a = b - a +b_minus_a_2 = np.subtract(b,a) diff --git a/Book4_Ch01_Python_Codes/Bk4_Ch1_05.py b/Book4_Ch01_Python_Codes/Bk4_Ch1_05.py new file mode 100644 index 0000000..4dba95d --- /dev/null +++ b/Book4_Ch01_Python_Codes/Bk4_Ch1_05.py @@ -0,0 +1,17 @@ + +############### +# Authored by Weisheng Jiang +# Book 4 | From Basic Arithmetic to Machine Learning +# Published and copyrighted by Tsinghua University Press +# Beijing, China, 2022 +############### + +# Bk4_Ch1_05.py + +import numpy as np + +# define a column vector +a = np.array([[2], [2]]) + +b = 2*a +c = -1.5*a