-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhw2_9.py
More file actions
51 lines (39 loc) · 1.24 KB
/
hw2_9.py
File metadata and controls
51 lines (39 loc) · 1.24 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
47
48
49
50
51
import numpy as np
np.set_printoptions(formatter={'float': '{: 0.3f}'.format})
N_OUT = 1000
N_IN = 1000
N_EXPERIMENTS = 1000
def f(X):
Y = np.ones(X.shape[0])
Y[np.sum(X**2,1)-0.6<0] = -1
# flip 10% of Y
Y[np.random.permutation(len(Y))[0:int(len(Y) / 10)]] *= -1
return Y
W = np.zeros((N_EXPERIMENTS,6))
E_in = np.zeros(N_EXPERIMENTS)
E_out = np.zeros(N_EXPERIMENTS)
def X2Z(X):
Z = np.column_stack((X, X[:, 0] * X[:, 1], X**2))
Z_b = np.column_stack((np.ones(Z.shape[0]), Z))
return (Z, Z_b)
for i in range(N_EXPERIMENTS):
X_in = np.random.uniform(-1.0, 1.0, (N_OUT, 2))
(Z_in, Z_in_b) = X2Z(X_in)
Y_in_f = f(X_in)
W[i] = np.matmul(np.matmul(np.linalg.inv(np.matmul(Z_in_b.T, Z_in_b)),Z_in_b.T),Y_in_f)
def g(X):
Y = np.ones(X.shape[0])
(Z, Z_b) = X2Z(X)
Y[np.matmul(Z_b, W[i])<0]=-1
return Y
Y_in_g = g(X_in)
E_in[i] = 1.0-np.sum(Y_in_g==Y_in_f)/N_IN
print(W[i])
X_out = np.random.uniform(-1.0, 1.0, (N_OUT, 2))
(Z_out, Z_out_b) = X2Z(X_out)
Y_out_f = f(X_out)
Y_out_g = g(X_out)
E_out[i] = 1.0 - np.sum(Y_out_g == Y_out_f) / N_OUT
print("mean of W:", np.mean(W,0))
print("mean of E_in:", np.mean(E_in))
print("mean of E_out:", np.mean(E_out))