forked from mazefeng/ml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperceptron.py
executable file
·101 lines (75 loc) · 2.99 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# coding=utf-8
import sys
import random
import math
import numpy as np
from common import read_dense_data
from common import map_label
random.seed(1024 * 1024)
class Perceptron:
def __init__(self):
self.w = None
def train(self, X, Y, alpha = 1.0, max_update = 1000, max_iter = 10000):
m, n = X.shape
I = range(m)
self.w = np.matrix(np.zeros([n, 1]))
update = 0
for iter in range(max_iter):
random.shuffle(I)
err = 0
for i in I:
if np.sign(X[i] * self.w) == Y[i]: continue
self.w += alpha * X[i].T * Y[i]
update += 1
if update > max_update: return
err += 1
print >> sys.stderr, 'Iter : %d\ttraining loss: %lf' % (iter, 1.0 * err / len(Y))
def test(self, X, Y):
Y_pred = np.sign(X * self.w)
P = np.matrix(np.zeros(Y.shape))
P[np.where(Y == Y_pred)] = 1
return 1.0 * P.sum() / len(Y)
class Pocket(Perceptron):
def train(self, X, Y, alpha = 1.0, max_update = 1000):
m, n = X.shape
self.w = np.matrix(np.zeros([n, 1]))
update = 0
min_err = m
w_test = np.matrix(np.zeros(self.w.shape))
while update <= max_update:
i = random.randint(0, m - 1)
if np.sign(X[i] * w_test) == Y[i]: continue
w_test += alpha * X[i].T * Y[i]
update += 1
P = np.matrix(np.zeros(Y.shape))
Y_pred = np.sign(X * w_test)
P[np.where(Y != Y_pred)] = 1
err = P.sum()
if update % 100 == 0:
print >> sys.stderr, 'Update : %d\ttraining loss: %lf' % (update, 1.0 * err / len(Y))
if err < min_err:
min_err = err
self.w = w_test
if __name__ == '__main__':
train_path = 'data/heart_scale.train'
test_path = 'data/heart_scale.test'
X_train, Y_train = read_dense_data(open(train_path))
X_test, Y_test = read_dense_data(open(test_path))
X_train = np.matrix(X_train)
Y_train = [int(y) for y in Y_train]
Y_train = np.matrix(Y_train).T
X_test = np.matrix(X_test)
Y_test = [int(y) for y in Y_test]
Y_test = np.matrix(Y_test).T
clf = Perceptron()
clf.train(X_train, Y_train)
acc_train = clf.test(X_train, Y_train)
acc_test = clf.test(X_test, Y_test)
print >> sys.stderr, 'Training accuracy for Perceptron : %lf%%' % (100 * acc_train)
print >> sys.stderr, 'Test accuracy for Perceptron : %lf%%' % (100 * acc_test)
clf = Pocket()
clf.train(X_train, Y_train)
acc_train = clf.test(X_train, Y_train)
acc_test = clf.test(X_test, Y_test)
print >> sys.stderr, 'Training accuracy for Pocket : %lf%%' % (100 * acc_train)
print >> sys.stderr, 'Test accuracy for Pocket : %lf%%' % (100 * acc_test)