forked from Visualize-ML/Book3_Elements-of-Mathematics
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBk3_Ch11_01.py
More file actions
43 lines (33 loc) · 1.11 KB
/
Bk3_Ch11_01.py
File metadata and controls
43 lines (33 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
###############
# Authored by Weisheng Jiang
# Book 3 | From Basic Arithmetic to Machine Learning
# Published and copyrighted by Tsinghua University Press
# Beijing, China, 2022
###############
# Bk3_Ch11_01
import numpy as np
import matplotlib.pyplot as plt
w_array = np.array([1/5,1/4,1/3,1/2,1,2,3,4,5])
x_array = np.linspace(-2,2,100)
ww, xx = np.meshgrid(w_array,x_array)
b = 0 # y intercept
ff = ww*xx + b
fig, ax = plt.subplots()
colors = plt.cm.jet(np.linspace(0,1,len(w_array)))
for i in np.linspace(1,len(w_array),len(w_array)):
plt.plot(x_array,ff[:,int(i)-1],
color = colors[int(i)-1],
label = '$w_1 = {lll:.2f}$'.format(lll = w_array[int(i)-1]))
plt.xlabel('x')
plt.ylabel('f(x)')
plt.legend()
plt.xticks(np.arange(-2, 2.5, step=0.5))
plt.yticks(np.arange(-2, 2.5, step=0.5))
plt.axis('scaled')
ax.set_xlim(-2,2)
ax.set_ylim(-2,2)
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.grid(linestyle='--', linewidth=0.25, color=[0.5,0.5,0.5])