-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode_student.py
More file actions
46 lines (37 loc) · 1.2 KB
/
code_student.py
File metadata and controls
46 lines (37 loc) · 1.2 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
44
45
46
WeChat: cstutorcs
QQ: 749389476
Email: tutorcs@163.com
## Question 2
import matplotlib.pyplot as plt
import numpy as np
from sklearn import datasets
X, y = datasets.make_circles(n_samples=200, factor=0.4, noise=0.04, random_state=13)
colors = np.array(['orange', 'blue'])
np.random.seed(123)
random_labeling = np.random.choice([0,1], size=X.shape[0], )
plt.scatter(X[:, 0], X[:, 1], s=20, color=colors[random_labeling])
plt.title("Randomly Labelled Points")
plt.savefig("Randomly_Labeled.png")
plt.show()
## Question 3
import numpy as np
import matplotlib.pyplot as plt
t_var = np.load("t_var.npy")
y_var = np.load("y_var.npy")
plt.plot(t_var, y_var)
plt.show()
def create_W(p):
## generate W which is a p-2 x p matrix as defined in the question
# your code here
return W
def loss(beta, y, W, L):
## compute loss for a given vector beta for data y, matrix W, regularization parameter L (lambda)
# your code here
return loss_val
## your code here
plt.plot(t_var, y_var, zorder=1, color='red', label='truth')
plt.plot(t_var, beta_hat, zorder=3, color='blue',
linewidth=2, linestyle='--', label='fit')
plt.legend(loc='best')
plt.title(f"L(beta_hat) = {loss(beta_hat, y, W, L)}")
plt.show()