From a722f548639b05f7735a90ac55eee84e55ef14e2 Mon Sep 17 00:00:00 2001 From: "Iris Series: Visualize Math -- From Arithmetic Basics to Machine Learning" <105787223+Visualize-ML@users.noreply.github.com> Date: Sat, 1 Feb 2025 17:01:57 +0800 Subject: [PATCH] Delete Book4_Ch13_Python_Codes directory --- Book4_Ch13_Python_Codes/Bk4_Ch13_01.py | 89 ----------------- Book4_Ch13_Python_Codes/Bk4_Ch13_02.py | 96 ------------------- Book4_Ch13_Python_Codes/Bk4_Ch13_03.py | 70 -------------- .../Streamlit_Bk4_Ch13_04.py | 90 ----------------- 4 files changed, 345 deletions(-) delete mode 100644 Book4_Ch13_Python_Codes/Bk4_Ch13_01.py delete mode 100644 Book4_Ch13_Python_Codes/Bk4_Ch13_02.py delete mode 100644 Book4_Ch13_Python_Codes/Bk4_Ch13_03.py delete mode 100644 Book4_Ch13_Python_Codes/Streamlit_Bk4_Ch13_04.py diff --git a/Book4_Ch13_Python_Codes/Bk4_Ch13_01.py b/Book4_Ch13_Python_Codes/Bk4_Ch13_01.py deleted file mode 100644 index fb601ea..0000000 --- a/Book4_Ch13_Python_Codes/Bk4_Ch13_01.py +++ /dev/null @@ -1,89 +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_Ch13_01.py - -import numpy as np -import matplotlib.pyplot as plt - -A = np.array([[1.25, -0.75], - [-0.75, 1.25]]) - -xx1, xx2 = np.meshgrid(np.linspace(-8, 8, 9), np.linspace(-8, 8, 9)) -num_vecs = np.prod(xx1.shape); - -thetas = np.linspace(0, 2*np.pi, num_vecs) - -thetas = np.reshape(thetas, (-1, 9)) -thetas = np.flipud(thetas); - -uu = np.cos(thetas); -vv = np.sin(thetas); - -fig, ax = plt.subplots() - -ax.quiver(xx1,xx2,uu,vv, - angles='xy', scale_units='xy',scale=1, - edgecolor='none', facecolor= 'b') - -plt.ylabel('$x_2$') -plt.xlabel('$x_1$') -plt.axis('scaled') -ax.set_xlim([-10, 10]) -ax.set_ylim([-10, 10]) -ax.grid(linestyle='--', linewidth=0.25, color=[0.5,0.5,0.5]) -ax.set_xticks(np.linspace(-10,10,11)); -ax.set_yticks(np.linspace(-10,10,11)); -plt.show() - -# Matrix multiplication -V = np.array([uu.flatten(),vv.flatten()]).T; -W = V@A; - -uu_new = np.reshape(W[:,0],(-1, 9)); -vv_new = np.reshape(W[:,1],(-1, 9)); - -fig, ax = plt.subplots() - -ax.quiver(xx1,xx2,uu,vv, - angles='xy', scale_units='xy',scale=1, - edgecolor='none', facecolor= 'b') - -ax.quiver(xx1,xx2,uu_new,vv_new, - angles='xy', scale_units='xy',scale=1, - edgecolor='none', facecolor= 'r') - -plt.ylabel('$x_2$') -plt.xlabel('$x_1$') -plt.axis('scaled') -ax.set_xlim([-10, 10]) -ax.set_ylim([-10, 10]) -ax.grid(linestyle='--', linewidth=0.25, color=[0.5,0.5,0.5]) -ax.set_xticks(np.linspace(-10,10,11)); -ax.set_yticks(np.linspace(-10,10,11)); -plt.show() - - -fig, ax = plt.subplots() -ax.quiver(xx1*0,xx2*0,uu,vv, - angles='xy', scale_units='xy',scale=1, - edgecolor='none', facecolor= 'b') - -ax.quiver(xx1*0,xx2*0,uu_new,vv_new, - angles='xy', scale_units='xy',scale=1, - edgecolor='none', facecolor= 'r') - -plt.ylabel('$x_2$') -plt.xlabel('$x_1$') -plt.axis('scaled') -ax.set_xlim([-2, 2]) -ax.set_ylim([-2, 2]) -ax.grid(linestyle='--', linewidth=0.25, color=[0.5,0.5,0.5]) -ax.set_xticks(np.linspace(-2,2,5)); -ax.set_yticks(np.linspace(-2,2,5)); -plt.show() diff --git a/Book4_Ch13_Python_Codes/Bk4_Ch13_02.py b/Book4_Ch13_Python_Codes/Bk4_Ch13_02.py deleted file mode 100644 index c18e6e1..0000000 --- a/Book4_Ch13_Python_Codes/Bk4_Ch13_02.py +++ /dev/null @@ -1,96 +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_Ch13_02.py - -import numpy as np -import matplotlib.pyplot as plt - -def visualize(X_circle,X_vec,title_txt): - - fig, ax = plt.subplots() - - plt.plot(X_circle[0,:], X_circle[1,:],'k', - linestyle = '--', - linewidth = 0.5) - - plt.quiver(0,0,X_vec[0,0],X_vec[1,0], - angles='xy', scale_units='xy',scale=1, - color = [0, 0.4392, 0.7529]) - - plt.quiver(0,0,X_vec[0,1],X_vec[1,1], - angles='xy', scale_units='xy',scale=1, - color = [1,0,0]) - - plt.axvline(x=0, color= 'k', zorder=0) - plt.axhline(y=0, color= 'k', zorder=0) - - plt.ylabel('$x_2$') - plt.xlabel('$x_1$') - - ax.set_aspect(1) - ax.set_xlim([-2.5, 2.5]) - ax.set_ylim([-2.5, 2.5]) - ax.grid(linestyle='--', linewidth=0.25, color=[0.5,0.5,0.5]) - ax.set_xticks(np.linspace(-2,2,5)); - ax.set_yticks(np.linspace(-2,2,5)); - plt.title(title_txt) - plt.show() - - -theta = np.linspace(0, 2*np.pi, 100) - -circle_x1 = np.cos(theta) -circle_x2 = np.sin(theta) - -V_vec = np.array([[np.sqrt(2)/2, -np.sqrt(2)/2], - [np.sqrt(2)/2, np.sqrt(2)/2]]) - -X_circle = np.array([circle_x1, circle_x2]) - -# plot original circle and two vectors -visualize(X_circle,V_vec,'Original') - -A = np.array([[1.25, -0.75], - [-0.75, 1.25]]) - -# plot the transformation of A - -visualize(A@X_circle, A@V_vec,'$A$') - - -#%% Eigen deomposition - -# A = V @ D @ V.T - -lambdas, V = np.linalg.eig(A) - -D = np.diag(np.flip(lambdas)) -V = V.T # reverse the order - -print('=== LAMBDA ===') -print(D) -print('=== V ===') -print(V) - -# plot the transformation of V.T - -visualize(V.T@X_circle, V.T@V_vec,'$V^T$') - -# plot the transformation of D @ V.T - -visualize(D@V.T@X_circle, D@V.T@V_vec,'$\u039BV^T$') - -# plot the transformation of V @ D @ V.T - -visualize(V@D@V.T@X_circle, V@D@V.T@V_vec,'$V\u039BV^T$') - -# plot the transformation of A - -visualize(A@X_circle, A@V_vec,'$A$') - diff --git a/Book4_Ch13_Python_Codes/Bk4_Ch13_03.py b/Book4_Ch13_Python_Codes/Bk4_Ch13_03.py deleted file mode 100644 index b9ea075..0000000 --- a/Book4_Ch13_Python_Codes/Bk4_Ch13_03.py +++ /dev/null @@ -1,70 +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_Ch13_03.py - -import numpy as np -import matplotlib.pyplot as plt - -theta = np.deg2rad(30) - -r = 0.8 # 1.2, scaling factor - -R = np.array([[np.cos(theta), -np.sin(theta)], - [np.sin(theta), np.cos(theta)]]) - -S = np.array([[r, 0], - [0, r]]) - -A = R@S - -# A = np.array([[1, -1], -# [1, 1]]) - -Lamb, V = np.linalg.eig(A) - -theta_array = np.arange(0,np.pi*2,np.pi*2/18) - - -colors = plt.cm.rainbow(np.linspace(0,1,len(theta_array))) - - -fig, ax = plt.subplots() - -for j, theat_i in enumerate(theta_array): - - # initial point - x = np.array([[5*np.cos(theat_i)], - [5*np.sin(theat_i)]]) - - plt.plot(x[0],x[1], - marker = 'x',color = colors_j, - markersize = 15) - # plot the initial point - - x_array = x - - for i in np.arange(20): - - x = A@x - x_array = np.column_stack((x_array,x)) - - - colors_j = colors[j,:] - plt.plot(x_array[0,:],x_array[1,:], - marker = '.',color = colors_j) - - -plt.axis('scaled') - -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.axvline(x=0,color = 'k') -ax.axhline(y=0,color = 'k') diff --git a/Book4_Ch13_Python_Codes/Streamlit_Bk4_Ch13_04.py b/Book4_Ch13_Python_Codes/Streamlit_Bk4_Ch13_04.py deleted file mode 100644 index e0e84a3..0000000 --- a/Book4_Ch13_Python_Codes/Streamlit_Bk4_Ch13_04.py +++ /dev/null @@ -1,90 +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 -import plotly.express as px -import pandas as pd - - -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: - - st.latex(r''' - A = \begin{bmatrix} - a & b\\ - c & d - \end{bmatrix}''') - - a = st.slider('a',-2.0, 2.0, step = 0.1, value = 1.0) - b = st.slider('b',-2.0, 2.0, step = 0.1, value = 0.0) - c = st.slider('c',-2.0, 2.0, step = 0.1, value = 0.0) - d = st.slider('d',-2.0, 2.0, step = 0.1, value = 1.0) - -#%% - -x1_ = np.linspace(-1, 1, 11) -x2_ = np.linspace(-1, 1, 11) - -xx1,xx2 = np.meshgrid(x1_, x2_) - -X = np.column_stack((xx1.flatten(), xx2.flatten())) - -# st.write(X) -A = np.array([[a, b], - [c, d]]) - -X = X@A - -# st.write(len(X)) -#%% -color_array = np.linspace(0,1,len(X)) -# st.write(color_array) -X = np.column_stack((X, color_array)) -df = pd.DataFrame(X, columns=['z1','z2', 'color']) - -#%% Scatter - -st.latex('A = ' + bmatrix(A)) - -fig = px.scatter(df, - x="z1", - y="z2", - color='color', - color_continuous_scale = 'rainbow') - -fig.update_layout( - autosize=False, - width=500, - height=500) - -fig.add_hline(y=0, line_color = 'black') -fig.add_vline(x=0, line_color = 'black') - -fig.update_xaxes(range=[-3, 3]) -fig.update_yaxes(range=[-3, 3]) -fig.update_coloraxes(showscale=False) - -st.plotly_chart(fig) - - -