-
Notifications
You must be signed in to change notification settings - Fork 101
/
perceptron_dual.py
55 lines (50 loc) · 1.65 KB
/
perceptron_dual.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
"""
This is the implementation of PerceptronDual,
which is accessible in https://github.com/FlameCharmander/MachineLearning,
accomplished by FlameCharmander,
and my csdn blog is https://blog.csdn.net/tudaodiaozhale,
contact me via 13030880@qq.com.
"""
import numpy as np
class PerceptronDual:
def __init__(self):
self.weights = None
self.bias = None
def sign(self, x): # sign function
return 1 if x >= 0 else -1
def train(self, data_set, labels):
lr = 1
n = np.array(data_set).shape[0] #n means rows
data_set = np.mat(data_set)
alpha = np.zeros(n) #alpha means the total iteration of wrong point
bias = 0
i = 0
while i < n:
#in this step, we elide gram matrix
if (labels[i] * self.sign(sum(alpha * labels * data_set * data_set[i].T)+bias) == -1):
alpha[i] = alpha[i] + lr
bias = bias + lr * labels[i]
i = 0
else:
i += 1
self.weights = sum(alpha * labels * data_set)
self.bias = bias
def predict(self, data):
data = np.array([data])
if (self.weights is not None and self.bias is not None):
return self.sign((self.weights * data.T) + self.bias)
else:
return 0
if __name__ == '__main__':
"""
this code is corresponding to algorithm(2.1) in P29
"""
data_set = [[3, 3],
[4, 3],
[1, 1]]
labels = [1, 1, -1]
perceptron = PerceptronDual()
perceptron.train(data_set, labels)
print(perceptron.weights)
result = perceptron.predict([1, 1])
print(result)