mirror of
https://github.com/Visualize-ML/Book4_Power-of-Matrix.git
synced 2026-05-05 12:21:07 +08:00
Add files via upload
This commit is contained in:
30
Book4_Ch01_Python_Codes/Bk3_Ch1_01.py
Normal file
30
Book4_Ch01_Python_Codes/Bk3_Ch1_01.py
Normal file
@@ -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
|
||||
###############
|
||||
|
||||
# Bk3_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()
|
||||
19
Book4_Ch01_Python_Codes/Bk3_Ch1_02.py
Normal file
19
Book4_Ch01_Python_Codes/Bk3_Ch1_02.py
Normal file
@@ -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
|
||||
###############
|
||||
|
||||
# Bk3_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)
|
||||
36
Book4_Ch01_Python_Codes/Bk3_Ch1_03.py
Normal file
36
Book4_Ch01_Python_Codes/Bk3_Ch1_03.py
Normal file
@@ -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
|
||||
###############
|
||||
|
||||
# Bk3_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()
|
||||
26
Book4_Ch01_Python_Codes/Bk3_Ch1_04.py
Normal file
26
Book4_Ch01_Python_Codes/Bk3_Ch1_04.py
Normal file
@@ -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
|
||||
###############
|
||||
|
||||
# Bk3_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)
|
||||
17
Book4_Ch01_Python_Codes/Bk3_Ch1_05.py
Normal file
17
Book4_Ch01_Python_Codes/Bk3_Ch1_05.py
Normal file
@@ -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
|
||||
###############
|
||||
|
||||
# Bk3_Ch1_05.py
|
||||
|
||||
import numpy as np
|
||||
|
||||
# define a column vector
|
||||
a = np.array([[2], [2]])
|
||||
|
||||
b = 2*a
|
||||
c = -1.5*a
|
||||
Reference in New Issue
Block a user