-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathand.py
65 lines (52 loc) · 1.48 KB
/
and.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import numpy as np
import matplotlib.pyplot as plt
X = np.array([
[0,0,-1],
[0,1,-1],
[1,0,-1],
[1,1,-1],
])
y = np.array([-1,-1,-1,1])
def perceptron_sgd(X, Y):
w = np.zeros(len(X[0]))
eta = 1
epochs = 20
for t in range(epochs):
for i, x in enumerate(X):
if (np.dot(X[i], w)*Y[i]) <= 0:
w = w + eta*X[i]*Y[i]
return w
w = perceptron_sgd(X,y)
print(w)
for d, sample in enumerate(X):
# Plot the negative samples
if d < 2:
plt.scatter(sample[0], sample[1], s=120, marker='_', linewidths=2)
# Plot the positive samples
else:
plt.scatter(sample[0], sample[1], s=120, marker='+', linewidths=2)
# Print a possible hyperplane, that is seperating the two classes.
plt.plot([-2,6],[6,0.5])
def perceptron_sgd_plot(X, Y):
'''
train perceptron and plot the total loss in each epoch.
:param X: data samples
:param Y: data labels
:return: weight vector as a numpy array
'''
w = np.zeros(len(X[0]))
eta = 1
n = 30
errors = []
for t in range(n):
total_error = 0
for i, x in enumerate(X):
if (np.dot(X[i], w)*Y[i]) <= 0:
total_error += (np.dot(X[i], w)*Y[i])
w = w + eta*X[i]*Y[i]
errors.append(total_error*-1)
plt.plot(errors)
plt.xlabel('Epoch')
plt.ylabel('Total Loss')
return w
perceptron_sgd_plot(X,y)