-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperceptron.py
79 lines (71 loc) · 2.54 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import numpy as np
def train_multiclass_perceptron(features, y_expected, classes):
r, c = features.shape
weights = np.random.rand(c, classes)
epochs = 0
while epochs < 1000:
error = 0
for i in range(r):
feature_vector = features[i, ::]
y_predicted = np.argmax(np.dot(feature_vector, weights))
if y_predicted == y_expected[i]:
continue
else:
error += 1
weights[::, y_expected[i]] = weights[::, y_expected[i]] + feature_vector.transpose()
weights[::, y_predicted] = weights[::, y_predicted] - feature_vector.transpose()
epochs += 1
print error / float(r)
return weights
def train_binary_perceptron(features, y_expected):
y_expected = [y if y == 1 else -1 for y in y_expected]
r, c = features.shape
weights = np.random.rand(c, 1)
epochs = 0
while epochs < 1000:
error = 0
for i in range(r):
feature_vector = features[i, ::]
y_predicted = np.dot(feature_vector, weights)
y_predicted = y_predicted[0]
if y_predicted >= 0:
y_predicted = 1
else:
y_predicted = -1
if y_predicted == y_expected[i]:
continue
else:
error += 1
weights = weights + y_expected[i] * feature_vector.transpose()
epochs += 1
print error / float(r)
return weights
def test_multiclass_perceptron(features, y_expected, classes, weights):
r, c = features.shape
error = 0
for i in range(r):
feature_vector = features[i, ::]
y_predicted = np.argmax(np.dot(feature_vector, weights))
print "expected = ", y_expected[i], "predicted = ", y_predicted
if y_predicted == y_expected[i]:
continue
else:
error += 1
print "Prediction accuracy = ", (1 - error / float(r)) * 100, "%"
def test_binary_perceptron(features, y_expected, classes, weights):
r, c = features.shape
error = 0
for i in range(r):
feature_vector = features[i, ::]
y_predicted = np.dot(feature_vector, weights)
y_predicted = y_predicted[0]
if y_predicted >= 0:
y_predicted = 1
else:
y_predicted = -1
print "expected = ", y_expected[i], "predicted = ", y_predicted
if y_predicted == y_expected[i]:
continue
else:
error += 1
print "Prediction accuracy = ", (1 - error / float(r)) * 100, "%"