diff --git a/Book4_Ch03_Python_Codes/Bk4_Ch3_01.py b/Book4_Ch03_Python_Codes/Bk4_Ch3_01.py new file mode 100644 index 0000000..d6c9906 --- /dev/null +++ b/Book4_Ch03_Python_Codes/Bk4_Ch3_01.py @@ -0,0 +1,52 @@ + +############### +# Authored by Weisheng Jiang +# Book 4 | From Basic Arithmetic to Machine Learning +# Published and copyrighted by Tsinghua University Press +# Beijing, China, 2022 +############### + +# Bk4_Ch3_01.py + +import matplotlib.pyplot as plt +import numpy as np + +p_values = [0.05, 0.2, 0.5, 1, 1.5, 2, 4, 8, np.inf] + +x1 = np.linspace(-2.5, 2.5, num=101); +x2 = x1; + +xx1, xx2 = np.meshgrid(x1,x2) + +fig, axes = plt.subplots(ncols=3,nrows=3, + figsize=(12, 12)) + +for p, ax in zip(p_values, axes.flat): + + if np.isinf(p): + zz = np.maximum(np.abs(xx1),np.abs(xx2)) + else: + zz = ((np.abs((xx1))**p) + (np.abs((xx2))**p))**(1./p) + + # plot contour of Lp + ax.contourf(xx1, xx2, zz, 20, cmap='RdYlBu_r') + + # plot contour of Lp = 1 + ax.contour (xx1, xx2, zz, [1], colors='k', linewidths = 2) + + # decorations + + ax.axhline(y=0, color='k', linewidth = 0.25) + ax.axvline(x=0, color='k', linewidth = 0.25) + ax.set_xlim(-2.5, 2.5) + ax.set_ylim(-2.5, 2.5) + 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_title('p = ' + str(p)) + ax.set_aspect('equal', adjustable='box') + +plt.show() diff --git a/Book4_Ch03_Python_Codes/Bk4_Ch3_02.py b/Book4_Ch03_Python_Codes/Bk4_Ch3_02.py new file mode 100644 index 0000000..e67950d --- /dev/null +++ b/Book4_Ch03_Python_Codes/Bk4_Ch3_02.py @@ -0,0 +1,53 @@ + +############### +# Authored by Weisheng Jiang +# Book 4 | From Basic Arithmetic to Machine Learning +# Published and copyrighted by Tsinghua University Press +# Beijing, China, 2022 +############### + +# Bk4_Ch3_02.py + +import numpy as np +from matplotlib import pyplot as plt +import seaborn as sns + +u = [0,0,4, 3] +v = [0,0,-2,4] +u_bis = [4,3,v[2],v[3]] +w = [0,0,2,7] + +fig, ax = plt.subplots() + +plt.quiver([u[0], u_bis[0], w[0]], + [u[1], u_bis[1], w[1]], + [u[2], u_bis[2], w[2]], + [u[3], u_bis[3], w[3]], + angles='xy', scale_units='xy', + scale=1, color=sns.color_palette()) + +plt.axvline(x=0, color='grey') +plt.axhline(y=0, color='grey') + +plt.text(3, 1, r'$||\vec{u}||_2$', + color=sns.color_palette()[0], size=12, + ha='center',va='center') + +plt.text(3, 6, r'$||\vec{v}||_2$', + color=sns.color_palette()[1], size=12, + ha='center',va='center') + +plt.text(0, 4, r'$||\vec{u}+\vec{v}||_2$', + color=sns.color_palette()[2], size=12, + ha='center',va='center') + +plt.ylabel('$x_2$') +plt.xlabel('$x_1$') +plt.axis('scaled') +ax.set_xticks(np.arange(-2,8 + 1)) +ax.set_yticks(np.arange(-2,8 + 1)) +ax.set_xlim(-2, 8) +ax.set_ylim(-2, 8) +ax.grid(linestyle='--', linewidth=0.25, color=[0.5,0.5,0.5]) + +# reference: Essential Math for Data Science diff --git a/Book4_Ch03_向量范数__数学要素__从加减乘除到机器学习.pdf b/Book4_Ch03_向量范数__数学要素__从加减乘除到机器学习.pdf new file mode 100644 index 0000000..caf5b42 Binary files /dev/null and b/Book4_Ch03_向量范数__数学要素__从加减乘除到机器学习.pdf differ diff --git a/Book4_Ch04_Python_Codes/Bk4_Ch4_01.py b/Book4_Ch04_Python_Codes/Bk4_Ch4_01.py new file mode 100644 index 0000000..068b44b --- /dev/null +++ b/Book4_Ch04_Python_Codes/Bk4_Ch4_01.py @@ -0,0 +1,41 @@ + +############### +# Authored by Weisheng Jiang +# Book 4 | From Basic Arithmetic to Machine Learning +# Published and copyrighted by Tsinghua University Press +# Beijing, China, 2022 +############### + +# Bk4_Ch4_01.py + +import numpy as np + +# 2d matrix +A_matrix = np.matrix([[2,4], + [6,8]]) +print(A_matrix.shape) +print(type(A_matrix)) + +# 1d array +A_1d = np.array([2,4]) +print(A_1d.shape) +print(type(A_1d)) + +# 2d array +A_2d = np.array([[2,4], + [6,8]]) +print(A_2d.shape) +print(type(A_2d)) + +# 3d array +A1 = [[2,4], + [6,8]] + +A2 = [[1,3], + [5,7]] + +A3 = [[1,0], + [0,1]] +A_3d = np.array([A1,A2,A3]) +print(A_3d.shape) +print(type(A_3d)) diff --git a/Book4_Ch04_Python_Codes/Bk4_Ch4_02.py b/Book4_Ch04_Python_Codes/Bk4_Ch4_02.py new file mode 100644 index 0000000..2cc94ff --- /dev/null +++ b/Book4_Ch04_Python_Codes/Bk4_Ch4_02.py @@ -0,0 +1,21 @@ + +############### +# Authored by Weisheng Jiang +# Book 4 | From Basic Arithmetic to Machine Learning +# Published and copyrighted by Tsinghua University Press +# Beijing, China, 2022 +############### + +# Bk4_Ch4_02.py + +import numpy as np + +A = np.matrix([[1,2,3], + [4,5,6], + [7,8,9]]) + +# extract diagonal elements +a = np.diag(A) + +# construct a diagonal matrix +A_diag = np.diag(a) diff --git a/Book4_Ch04_Python_Codes/Bk4_Ch4_03.py b/Book4_Ch04_Python_Codes/Bk4_Ch4_03.py new file mode 100644 index 0000000..16d7dbb --- /dev/null +++ b/Book4_Ch04_Python_Codes/Bk4_Ch4_03.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_Ch4_03.py + +import numpy as np + +# define matrix +A = np.matrix([[1, 2], [3, 4]]) +B = np.matrix([[2, 6], [4, 8]]) + +# matrix addition +A_plus_B = np.add(A,B) +A_plus_B_2 = A + B + + +# matrix subtraction +A_minus_B = np.subtract(A,B) +A_minus_B_2 = A - B diff --git a/Book4_Ch04_Python_Codes/Bk4_Ch4_04.py b/Book4_Ch04_Python_Codes/Bk4_Ch4_04.py new file mode 100644 index 0000000..fd52361 --- /dev/null +++ b/Book4_Ch04_Python_Codes/Bk4_Ch4_04.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_Ch4_04.py + +import numpy as np + +k = 2 +X = [[1,2], + [3,4]] + +# scalar multiplication +k_times_X = np.dot(k,X) +k_times_X_2 = k*np.matrix(X) diff --git a/Book4_Ch04_Python_Codes/Bk4_Ch4_05.py b/Book4_Ch04_Python_Codes/Bk4_Ch4_05.py new file mode 100644 index 0000000..7f5784e --- /dev/null +++ b/Book4_Ch04_Python_Codes/Bk4_Ch4_05.py @@ -0,0 +1,41 @@ + +############### +# Authored by Weisheng Jiang +# Book 4 | From Basic Arithmetic to Machine Learning +# Published and copyrighted by Tsinghua University Press +# Beijing, China, 2022 +############### + +# Bk4_Ch4_05.py + +import numpy as np + +# define matrix +A = np.matrix([[1, 2], + [3, 4], + [5, 6]]) + +# scaler +k = 2; + +# column vector c +c = np.array([[3], + [2], + [1]]) + +# row vector r +r = np.array([[2,1]]) + +# broadcasting principles + +# matrix A plus scalar k +A_plus_k = A + k + +# matrix A plus column vector c +A_plus_a = A + c + +# matrix A plus row vector r +A_plus_r = A + r + +# column vector c plus row vector r +c_plus_r = c + r diff --git a/Book4_Ch04_Python_Codes/Bk4_Ch4_06.py b/Book4_Ch04_Python_Codes/Bk4_Ch4_06.py new file mode 100644 index 0000000..e25aa6c --- /dev/null +++ b/Book4_Ch04_Python_Codes/Bk4_Ch4_06.py @@ -0,0 +1,21 @@ + +############### +# Authored by Weisheng Jiang +# Book 4 | From Basic Arithmetic to Machine Learning +# Published and copyrighted by Tsinghua University Press +# Beijing, China, 2022 +############### + +# Bk4_Ch4_06.py + +import numpy as np + +A = np.array([[1, 2], + [3, 4]]) + +B = np.array([[2, 4], + [1, 3]]) + +# matrix multiplication +A_times_B = np.matmul(A, B) +A_times_B_2 = A@B diff --git a/Book4_Ch04_Python_Codes/Bk4_Ch4_07.py b/Book4_Ch04_Python_Codes/Bk4_Ch4_07.py new file mode 100644 index 0000000..bf622bb --- /dev/null +++ b/Book4_Ch04_Python_Codes/Bk4_Ch4_07.py @@ -0,0 +1,32 @@ + +############### +# Authored by Weisheng Jiang +# Book 4 | From Basic Arithmetic to Machine Learning +# Published and copyrighted by Tsinghua University Press +# Beijing, China, 2022 +############### + +# Bk4_Ch4_07.py + +import numpy as np + +A = np.array([[1, 2]]) + +B = np.array([[5, 6], + [8, 9]]) + +print(A*B) + +A = np.array([[1, 2]]) + +B = np.matrix([[5, 6], + [8, 9]]) + +print(A*B) + +A = np.matrix([[1, 2]]) + +B = np.matrix([[5, 6], + [8, 9]]) + +print(A*B) diff --git a/Book4_Ch04_Python_Codes/Bk4_Ch4_08.py b/Book4_Ch04_Python_Codes/Bk4_Ch4_08.py new file mode 100644 index 0000000..99f8d14 --- /dev/null +++ b/Book4_Ch04_Python_Codes/Bk4_Ch4_08.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_Ch4_08.py + +from numpy.linalg import matrix_power as pw +A = np.array([[1., 2.], + [3., 4.]]) + +# matrix inverse +A_3 = pw(A,3) +A_3_v3 = A@A@A + +# piecewise power +A_3_piecewise = A**3 diff --git a/Book4_Ch04_Python_Codes/Bk4_Ch4_09.py b/Book4_Ch04_Python_Codes/Bk4_Ch4_09.py new file mode 100644 index 0000000..c32324c --- /dev/null +++ b/Book4_Ch04_Python_Codes/Bk4_Ch4_09.py @@ -0,0 +1,21 @@ + +############### +# Authored by Weisheng Jiang +# Book 4 | From Basic Arithmetic to Machine Learning +# Published and copyrighted by Tsinghua University Press +# Beijing, China, 2022 +############### + +# Bk4_Ch4_09.py + +import numpy as np + +A = np.matrix([[1,3], + [2,4]]) + +print(A**2) + +B = np.array([[1,3], + [2,4]]) + +print(B**2) diff --git a/Book4_Ch04_Python_Codes/Bk4_Ch4_10.py b/Book4_Ch04_Python_Codes/Bk4_Ch4_10.py new file mode 100644 index 0000000..ba829f1 --- /dev/null +++ b/Book4_Ch04_Python_Codes/Bk4_Ch4_10.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_Ch4_10.py + +import numpy as np + +A = np.array([[1, 2], + [3, 4], + [5, 6]]) + +# matrix transpose +A_T = A.transpose() +A_T_2 = A.T diff --git a/Book4_Ch04_Python_Codes/Bk4_Ch4_11.py b/Book4_Ch04_Python_Codes/Bk4_Ch4_11.py new file mode 100644 index 0000000..5bf7fa6 --- /dev/null +++ b/Book4_Ch04_Python_Codes/Bk4_Ch4_11.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_Ch4_11.py + +from numpy.linalg import inv +A = np.array([[1., 2.], + [3., 4.]]) + +# matrix inverse +A_inverse = inv(A) +A_times_A_inv = A@A_inverse diff --git a/Book4_Ch04_Python_Codes/Bk4_Ch4_12.py b/Book4_Ch04_Python_Codes/Bk4_Ch4_12.py new file mode 100644 index 0000000..ab43f06 --- /dev/null +++ b/Book4_Ch04_Python_Codes/Bk4_Ch4_12.py @@ -0,0 +1,21 @@ + +############### +# Authored by Weisheng Jiang +# Book 4 | From Basic Arithmetic to Machine Learning +# Published and copyrighted by Tsinghua University Press +# Beijing, China, 2022 +############### + +# Bk4_Ch4_12.py + +import numpy as np + +A = np.matrix([[1, 2], + [3, 4]]) + +print(A.I) + +B = np.array([[1, 2], + [3, 4]]) + +print(B.I) diff --git a/Book4_Ch04_Python_Codes/Bk4_Ch4_13.py b/Book4_Ch04_Python_Codes/Bk4_Ch4_13.py new file mode 100644 index 0000000..861fb7a --- /dev/null +++ b/Book4_Ch04_Python_Codes/Bk4_Ch4_13.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_Ch4_13.py + +import numpy as np +A = np.array([[1, -1, 0], + [3, 2, 4], + [-2, 0, 3]]) + +# calculate trace of A +tr_A = np.trace(A) diff --git a/Book4_Ch04_Python_Codes/Bk4_Ch4_14.py b/Book4_Ch04_Python_Codes/Bk4_Ch4_14.py new file mode 100644 index 0000000..990e7f1 --- /dev/null +++ b/Book4_Ch04_Python_Codes/Bk4_Ch4_14.py @@ -0,0 +1,21 @@ + +############### +# Authored by Weisheng Jiang +# Book 4 | From Basic Arithmetic to Machine Learning +# Published and copyrighted by Tsinghua University Press +# Beijing, China, 2022 +############### + +# Bk4_Ch4_14.py + +import numpy as np + +A = np.array([[1,2], + [3,4]]) + +B = np.array([[5,6], + [7,8]]) + +# Hadamard product +A_times_B_piecewise = np.multiply(A,B) +A_times_B_piecewise_V2 = A*B diff --git a/Book4_Ch04_Python_Codes/Bk4_Ch4_15.py b/Book4_Ch04_Python_Codes/Bk4_Ch4_15.py new file mode 100644 index 0000000..8fa92de --- /dev/null +++ b/Book4_Ch04_Python_Codes/Bk4_Ch4_15.py @@ -0,0 +1,16 @@ + +############### +# Authored by Weisheng Jiang +# Book 4 | From Basic Arithmetic to Machine Learning +# Published and copyrighted by Tsinghua University Press +# Beijing, China, 2022 +############### + +# Bk4_Ch4_15.py + +import numpy as np +A = np.array([[3, 1], + [2, 4]]) + +# calculate determinant of A +det_A = np.linalg.det(A) diff --git a/Book4_Ch04_矩阵__数学要素__从加减乘除到机器学习.pdf b/Book4_Ch04_矩阵__数学要素__从加减乘除到机器学习.pdf new file mode 100644 index 0000000..8c2a6e7 Binary files /dev/null and b/Book4_Ch04_矩阵__数学要素__从加减乘除到机器学习.pdf differ diff --git a/Book4_Ch05_矩阵乘法__数学要素__从加减乘除到机器学习.pdf b/Book4_Ch05_矩阵乘法__数学要素__从加减乘除到机器学习.pdf new file mode 100644 index 0000000..9ae84bd Binary files /dev/null and b/Book4_Ch05_矩阵乘法__数学要素__从加减乘除到机器学习.pdf differ diff --git a/Book4_Ch06_Python_Codes/Bk4_Ch6_01.py b/Book4_Ch06_Python_Codes/Bk4_Ch6_01.py new file mode 100644 index 0000000..bcfc7a2 --- /dev/null +++ b/Book4_Ch06_Python_Codes/Bk4_Ch6_01.py @@ -0,0 +1,33 @@ + +############### +# Authored by Weisheng Jiang +# Book 4 | From Basic Arithmetic to Machine Learning +# Published and copyrighted by Tsinghua University Press +# Beijing, China, 2022 +############### + + +# Bk4_Ch6_01.py + +import numpy as np + +A = np.array([[1, 2, 3, 0, 0], + [4, 5, 6, 0, 0], + [0, 0, 0, -1, 0], + [0, 0 ,0, 0, 1]]) + +# NumPy array slicing + +A_1_1 = A[0:2,0:3] + +A_1_2 = A[0:2,3:] +# A_1_2 = A[0:2,-2:] +A_2_1 = A[2:,0:3] +# A_2_1 = A[-2:,0:3] +A_2_2 = A[2:,3:] +# A_2_2 = A[-2:,-2:] + +# Assemble a matrix from nested lists of blocks + +A_ = np.block([[A_1_1, A_1_2], + [A_2_1, A_2_2]]) diff --git a/Book4_Ch06_Python_Codes/Bk4_Ch6_02.py b/Book4_Ch06_Python_Codes/Bk4_Ch6_02.py new file mode 100644 index 0000000..1c53a13 --- /dev/null +++ b/Book4_Ch06_Python_Codes/Bk4_Ch6_02.py @@ -0,0 +1,54 @@ + +############### +# Authored by Weisheng Jiang +# Book 4 | From Basic Arithmetic to Machine Learning +# Published and copyrighted by Tsinghua University Press +# Beijing, China, 2022 +############### + + +# Bk4_Ch6_02.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) + +# Generate matrices A and B +A = np.random.random_integers(0,40,size=(6,4)) +A = A/20 - 1 + +B = np.random.random_integers(0,40,size=(4,3)) +B = B/20 - 1 + +# visualize matrix A and B +plot_heatmap(A,'A') + +plot_heatmap(B,'B') + +# visualize A@B +C = A@B +plot_heatmap(C,'C = AB') + +C_rep = np.zeros_like(C) + +# reproduce C + +for i in np.arange(4): + C_i = A[:,[i]]@B[[i],:]; + title = 'C' + str(i + 1) + plot_heatmap(C_i,title) + + C_rep = C_rep + C_i + +# Visualize reproduced C +plot_heatmap(C_rep,'C reproduced') + diff --git a/Book4_Ch06_Python_Codes/Bk4_Ch6_03.py b/Book4_Ch06_Python_Codes/Bk4_Ch6_03.py new file mode 100644 index 0000000..1fd7910 --- /dev/null +++ b/Book4_Ch06_Python_Codes/Bk4_Ch6_03.py @@ -0,0 +1,50 @@ + +############### +# Authored by Weisheng Jiang +# Book 4 | From Basic Arithmetic to Machine Learning +# Published and copyrighted by Tsinghua University Press +# Beijing, China, 2022 +############### + + +# Bk4_Ch6_03.py + +import numpy as np +from matplotlib import pyplot as plt +import seaborn as sns + +A = np.array([[-1, 1], + [0.7, -0.4]]) + +B = np.array([[0.5, -0.6], + [-0.8, 0.3]]) + +A_kron_B = np.kron(A, B) + +fig, axs = plt.subplots(1, 5, figsize=(12, 5)) + +plt.sca(axs[0]) +ax = sns.heatmap(A,cmap='RdYlBu_r',vmax = 1,vmin = -1, + cbar_kws={"orientation": "horizontal"}) +ax.set_aspect("equal") +plt.title('A') + +plt.sca(axs[1]) +plt.title('$\otimes$') +plt.axis('off') + +plt.sca(axs[2]) +ax = sns.heatmap(B,cmap='RdYlBu_r',vmax = 1,vmin = -1, + cbar_kws={"orientation": "horizontal"}) +ax.set_aspect("equal") +plt.title('B') + +plt.sca(axs[3]) +plt.title('=') +plt.axis('off') + +plt.sca(axs[4]) +ax = sns.heatmap(A_kron_B,cmap='RdYlBu_r',vmax = 1,vmin = -1, + cbar_kws={"orientation": "horizontal"}) +ax.set_aspect("equal") +plt.title('C') diff --git a/Book4_Ch06_分块矩阵__数学要素__从加减乘除到机器学习.pdf b/Book4_Ch06_分块矩阵__数学要素__从加减乘除到机器学习.pdf new file mode 100644 index 0000000..cf7fbbe Binary files /dev/null and b/Book4_Ch06_分块矩阵__数学要素__从加减乘除到机器学习.pdf differ diff --git a/Book4_Ch07_向量空间__数学要素__从加减乘除到机器学习.pdf b/Book4_Ch07_向量空间__数学要素__从加减乘除到机器学习.pdf new file mode 100644 index 0000000..20635be Binary files /dev/null and b/Book4_Ch07_向量空间__数学要素__从加减乘除到机器学习.pdf differ