diff --git a/Book4_Ch02_Python_Codes/Bk4_Ch2_01.py b/Book4_Ch02_Python_Codes/Bk4_Ch2_01.py deleted file mode 100644 index 1949300..0000000 --- a/Book4_Ch02_Python_Codes/Bk4_Ch2_01.py +++ /dev/null @@ -1,30 +0,0 @@ - -############### -# 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 -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_Ch02_Python_Codes/Bk4_Ch2_02.py b/Book4_Ch02_Python_Codes/Bk4_Ch2_02.py deleted file mode 100644 index c7aaa78..0000000 --- a/Book4_Ch02_Python_Codes/Bk4_Ch2_02.py +++ /dev/null @@ -1,19 +0,0 @@ - -############### -# 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 - -# 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_Ch02_Python_Codes/Bk4_Ch2_03.py b/Book4_Ch02_Python_Codes/Bk4_Ch2_03.py deleted file mode 100644 index d16adf1..0000000 --- a/Book4_Ch02_Python_Codes/Bk4_Ch2_03.py +++ /dev/null @@ -1,36 +0,0 @@ - -############### -# 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_Ch02_Python_Codes/Bk4_Ch2_04.py b/Book4_Ch02_Python_Codes/Bk4_Ch2_04.py deleted file mode 100644 index a9e05db..0000000 --- a/Book4_Ch02_Python_Codes/Bk4_Ch2_04.py +++ /dev/null @@ -1,26 +0,0 @@ - -############### -# 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_Ch02_Python_Codes/Bk4_Ch2_05.py b/Book4_Ch02_Python_Codes/Bk4_Ch2_05.py deleted file mode 100644 index 45ced0c..0000000 --- a/Book4_Ch02_Python_Codes/Bk4_Ch2_05.py +++ /dev/null @@ -1,17 +0,0 @@ - -############### -# 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 - -import numpy as np - -# define a column vector -a = np.array([[2], [2]]) - -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 deleted file mode 100644 index 63933d7..0000000 --- a/Book4_Ch02_Python_Codes/Bk4_Ch2_06.py +++ /dev/null @@ -1,20 +0,0 @@ - -############### -# 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([[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_07.py b/Book4_Ch02_Python_Codes/Bk4_Ch2_07.py deleted file mode 100644 index 773c9cc..0000000 --- a/Book4_Ch02_Python_Codes/Bk4_Ch2_07.py +++ /dev/null @@ -1,19 +0,0 @@ - -############### -# 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,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_08.py b/Book4_Ch02_Python_Codes/Bk4_Ch2_08.py deleted file mode 100644 index 2e86111..0000000 --- a/Book4_Ch02_Python_Codes/Bk4_Ch2_08.py +++ /dev/null @@ -1,19 +0,0 @@ - -############### -# 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 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_09.py b/Book4_Ch02_Python_Codes/Bk4_Ch2_09.py deleted file mode 100644 index 7d64826..0000000 --- a/Book4_Ch02_Python_Codes/Bk4_Ch2_09.py +++ /dev/null @@ -1,22 +0,0 @@ - -############### -# 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 deleted file mode 100644 index a247aee..0000000 --- a/Book4_Ch02_Python_Codes/Bk4_Ch2_10.py +++ /dev/null @@ -1,37 +0,0 @@ - -############### -# 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 deleted file mode 100644 index 1404f15..0000000 --- a/Book4_Ch02_Python_Codes/Bk4_Ch2_11.py +++ /dev/null @@ -1,24 +0,0 @@ - -############### -# 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 deleted file mode 100644 index d3f65d4..0000000 --- a/Book4_Ch02_Python_Codes/Bk4_Ch2_12.py +++ /dev/null @@ -1,27 +0,0 @@ - -############### -# 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 deleted file mode 100644 index 43a539a..0000000 --- a/Book4_Ch02_Python_Codes/Bk4_Ch2_13.py +++ /dev/null @@ -1,40 +0,0 @@ - -############### -# 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') diff --git a/Book4_Ch02_Python_Codes/Streamlit_Bk4_Ch2_13.py b/Book4_Ch02_Python_Codes/Streamlit_Bk4_Ch2_13.py deleted file mode 100644 index ecec3ad..0000000 --- a/Book4_Ch02_Python_Codes/Streamlit_Bk4_Ch2_13.py +++ /dev/null @@ -1,88 +0,0 @@ - -############### -# Authored by Weisheng Jiang -# Book 4 | From Basic Arithmetic to Machine Learning -# Published and copyrighted by Tsinghua University Press -# Beijing, China, 2022 -############### - -import streamlit as st -import numpy as np -from plotly.subplots import make_subplots -import plotly.graph_objects as go -import plotly.express as px - -def bmatrix(a): - """Returns a LaTeX bmatrix - - :a: numpy array - :returns: LaTeX bmatrix as a string - """ - if len(a.shape) > 2: - raise ValueError('bmatrix can at most display two dimensions') - lines = str(a).replace('[', '').replace(']', '').splitlines() - rv = [r'\begin{bmatrix}'] - rv += [' ' + ' & '.join(l.split()) + r'\\' for l in lines] - rv += [r'\end{bmatrix}'] - return '\n'.join(rv) - -#%% -with st.sidebar: - - num_a = st.slider('Number of rows, a:', - 3,6,step = 1) - num_b = st.slider('Number of rows, b:', - 3,6,step = 1) - -a = np.random.uniform(0,1,num_a).reshape((-1,1)) -a = np.round(a,1) -b = np.random.uniform(0,1,num_b).reshape((-1,1)) -b = np.round(b,1) - -show_number = False -with st.sidebar: - show_number = st.checkbox('Display values') - -tensor_a_b = a@b.T - -#%% visualization - -st.latex('a = ' + bmatrix(a)) -st.latex('b = ' + bmatrix(b)) -st.latex('a \otimes b = ab^{T}') -st.latex( bmatrix(a) + '@' + bmatrix(b.T) + ' = ' + bmatrix(tensor_a_b)) -col1, col2, col3 = st.columns(3) - -with col1: - fig_a = px.imshow(a, text_auto=show_number, - color_continuous_scale='viridis', - aspect = 'equal') - - fig_a.update_layout(height=400, width=300) - fig_a.layout.coloraxis.showscale = False - st.plotly_chart(fig_a) - -with col2: - fig_b = px.imshow(b, text_auto=show_number, - color_continuous_scale='viridis', - aspect = 'equal') - - fig_b.update_layout(height=400, width=300) - fig_b.layout.coloraxis.showscale = False - st.plotly_chart(fig_b) - -with col3: - fig_ab = px.imshow(tensor_a_b, text_auto=show_number, - color_continuous_scale='viridis', - aspect = 'equal') - - fig_ab.update_layout(height=400, width=400) - - fig_ab.layout.coloraxis.showscale = False - st.plotly_chart(fig_ab) - - - - - -