原理
特别注意的是,感知机只适用于二分类。
Python 实现
"""
-*- coding: utf-8 -*-
@ Time : 2021/8/9 22:10
@ Author : Raymond
@ Email : wanght2316@163.com
@ Editor : Pycharm
"""
import numpy as np
import matplotlib.pyplot as plt
data = [[1, 4], [0.5, 2], [2, 2.3], [1, 0.5], [2, 1], [4, 1], [3.5, 4], [3, 2.2]]
label = [1, 1, 1, -1, -1, -1, 1, -1]
data = np.array(data)
label = np.array(label)
# 初始化w, b, lr
w = np.array([0, 0])
b = 0
lr = 1
# 计算 y * (wx + b)
f = (np.dot(data, w.T) + b) * label
idx = np.where(f <= 0)
# 用SGD迭代求解 w, b
epoch = 1
while f[idx].size != 0:
point = np.random.randint((f[idx].shape[0]))
x = data[idx[0][point], :]
y = label[idx[0][point]]
w = w + lr * y * x
b = b + lr * y
print('epoch: {}----w: {}----b: {}'.format(epoch, w, b))
f = (np.dot(data, w.T) + b) * label
idx = np.where(f <= 0)
epoch += 1
# 绘图
x1 = np.arange(0, 6, 0.1)
x2 = (w[0] * x1 + b) / (-w[1])
idx_p = np.where(label == 1)
idx_n = np.where(label != 1)
data_p = data[idx_p]
data_n = data[idx_n]
plt.scatter(data_p[:, 0], data_p[:, 1], color='red')
plt.scatter(data_n[:, 0], data_n[:, 1], color='green')
plt.title('Preception algorithm', fontsize=16)
plt.plot(x1, x2)
plt.savefig('./precepion.jpg')
plt.show()
print('Perception algorithm is over')
效果
epoch: 1----w: [-4. -1.]----b: -1
epoch: 2----w: [-2. 1.3]----b: 0
epoch: 3----w: [0. 3.6]----b: 1
epoch: 4----w: [-3. 1.4]----b: 0
epoch: 5----w: [-1. 3.7]----b: 1
epoch: 6----w: [-2. 3.2]----b: 0
epoch: 7----w: [-5. 1.]----b: -1
epoch: 8----w: [-4. 5.]----b: 0
Perception algorithm is over
小白初学,说的不明白的地方请大家批评指正。