0%

白话机器学习的数学——分类——感知机

判别函数

\[ f_\omega(x) = \begin{cases} 1 & (\omega \cdot x \geq 0)\\ -1 & (\omega \cdot x < 0) \end{cases} \]

权重向量的更新表达式

\[ \omega := \begin{cases} \omega + y^{(i)}x^{(i)} & (f_\omega(x^{(i)}) \neq y^{(i)})\\ \omega & (f_\omega(x^{(i)}) = y^{(i)}) \end{cases} \]

使权重向量成为法线向量的直线

\[ \omega \cdot x = \omega_1x_1 + \omega_2x_2 = 0 \]

\[ x_2 = -\frac{\omega_1}{\omega_2}x_1 \]

代码示例

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
import numpy as np
import matplotlib.pyplot as plt

# 训练数据
train = np.loadtxt('images1.csv', delimiter=',', skiprows=1)
train_x = train[:, 0:2]
train_y = train[:, 2]

# 权重初始化
w = np.random.rand(2)

# 判别函数
def f(x):
if np.dot(w, x) >= 0:
return 1
else:
return -1

# 重复次数
epoch = 10

# 更新次数
count = 0

# 学习权重
for _ in range(epoch):
for x, y in zip(train_x, train_y):
if f(x) != y:
w = w + y * x
# 输出日志
count += 1
print('第 {} 次 : w = {}'.format(count, w))

# 绘图确认
plt.plot(train_x[train_y == 1, 0], train_x[train_y == 1, 1], 'o')
plt.plot(train_x[train_y == -1, 0], train_x[train_y == -1, 1], 'x')
x1 = np.arange(0, 500)
plt.plot(x1, -w[0] / w[1] * x1, linestyle='dashed')
plt.show()