感知机(手写原理)及其Python实现

原理

感知机原理
特别注意的是,感知机只适用于二分类

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

perception效果

小白初学,说的不明白的地方请大家批评指正。

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值