forked from swing-research/prbook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
perceptron.py
50 lines (34 loc) · 1.26 KB
/
perceptron.py
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
import numpy as np
def train(features, labels, lr=0.1, n_iter=10):
n, d = features.shape
assert len(labels) == n
# initialize weights
w = np.zeros((d,))
# NB: if all is zero-mean, we are scale-free.
# With bias things would change.
for i_iter in range(10):
for i_example in range(n):
y_hat = np.sign(np.dot(features[i_example], w))
w += lr * (labels[i_example] - y_hat) * features[i_example]
return w
if __name__ == "__main__":
import matplotlib.pyplot as plt
# you can also illustrate the difference between
# random and non-random patterns (like mnist)! (better bounds?)
n = 300
d = 160
X = np.random.randn(n, d)
# assign random classes (use a normal function for binary
# random)
y = np.sign(np.random.randn(n))
w = train(X, y, n_iter=100)
print("The cos(angle) between the true and the estimated label vector is:",
np.dot(np.sign(X @ w), y) / n)
w_orth = np.random.randn(d)
w_orth -= w * np.dot(w_orth, w) / np.linalg.norm(w)**2
print(w_orth.shape)
X_w = X @ w / np.linalg.norm(w)
X_orth = X @ w_orth / np.linalg.norm(w_orth)
plt.scatter(X_orth[y==1], X_w[y==1])
plt.scatter(X_orth[y==-1], X_w[y==-1])
plt.show()