感知机和对偶

感知机以及对偶求解的代码实现

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
import numpy as np
import matplotlib.pyplot as plt
def check(x, y, alpha, b):
# calculate gram matrix
gram = x.dot(x.T)
length = y.shape[0]
# too check the classify condition
for i in range(length):
s = y[i]*(sum([alpha[j]*y[j]*gram[j][i] for j in range(length)]) + b)
if s <= 0:
return i
# prepare data
np.random.seed(10)
x = np.random.randint(0, 100, (6, 2))
y = np.random.choice([-1, 1], 6)
# init alpha
alpha = np.random.randint(0, 10, 6)
# init b
b = 10
flag = True
while flag:
out = check(x, y, alpha, b)
if out is not None:
alpha[out] += 1
b += y[out]
else:
flag = False
# generate w
w = (y*alpha).T.dot(x)
for i in range(len(x)):
if y[i] == 1:
plt.scatter(x[i][0], x[i][1], color="red")
if y[i] == -1:
plt.scatter(x[i][0], x[i][1], color="green")
xx = np.linspace(np.min(x), np.max(x), 100)
yy = (-b - w[0] * xx)/ w[1]
plt.plot(xx, yy, linewidth=2)
plt.show()

人艰不拆,生活不易