-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathperceptron.py
More file actions
61 lines (48 loc) · 1.38 KB
/
Copy pathperceptron.py
File metadata and controls
61 lines (48 loc) · 1.38 KB
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
import numpy as np
X = np.array([[3, 3], [4, 3], [1, 1]])
Y = np.array([1, 1, -1])
theta = 1
# 原始形式
def fit(X, Y):
w = np.zeros([2, 1])
b = 0
while True:
true_point = 0
for i in range(len(X)):
if (Y[i] * (np.dot(w.T, X[i]) + b))[0] <= 0:
w = w + theta * Y[i] * X[i].reshape(-1, 1)
b = b + theta * Y[i]
print('w: %s' % w)
print('b: %s\n' % b)
else:
true_point += 1
if true_point == len(X):
break
return w, b
# 对偶形式
def fit2(X, Y):
alpha = np.zeros([len(X), 1])
b = 0
Gram = np.empty([len(X), len(X)])
for i in range(len(X)):
for j in range(len(X)):
Gram[i, j] = np.dot(X[i], X[j])
while True:
true_point = 0
for i in range(len(X)):
# 这里的[0]是为了将array转换成数字
if (Y[i] * (np.sum(alpha[j] * Y[j] * Gram[i, j] for j in range(len(X))) + b))[0] <= 0:
alpha[i] += theta
b += Y[i]
print('alpha: %s' % alpha)
print('b: %s\n' % b)
else:
true_point += 1
if true_point == len(X):
break
w = np.sum(alpha[i] * Y[i] * X[i] for i in range(len(X)))
return w, b
w, b = fit2(X, Y)
print('\n----result----')
print(w)
print(b)