感知机的简单实现

导入库

import numpy as np
import random
import matplotlib.pyplot as plt

数据集

x, y = [], []
for i in range(500):
    r = random.randint(0, 100) * 0.1
    delta = random.randint(-500, 500) * 0.1
    if delta != 0:
        x.append([r, r+delta])
        if delta >= 0:
            y.append(1)
        else:
            y.append(-1)
print(len(x))
print(len(y))
print(x[:5])
print(y[:5])
499
499
[[4.6000000000000005, 26.6], [9.700000000000001, -18.4], [4.4, 13.9], [0.6000000000000001, -33.9], [3.6, 35.4]]
[1, -1, 1, -1, 1]
x_pos, x_neg = [], []
y_pos, y_neg = [], []
for a, b in x:
    if a < b:
        x_pos.append(a)
        y_pos.append(b)
    else:
        x_neg.append(a)
        y_neg.append(b)
plt.scatter(x_pos, y_pos)
plt.scatter(x_neg, y_neg)
plt.show()

模型

class Perceptron(object):
    
    def __init__(self, dim, lr):
        self.w = np.zeros(dim)
        self.b = 0
        self.lr = lr
        
    def train(self, x, y):
        if isinstance(x, list):
            x = np.array(x)
        if isinstance(y, list):
            y = np.array(y)
        for _ in range(10):
            for i in range(x.shape[0]):
                if y[i] * (np.sum(self.w * x[i]) + b) <= 0:
                    self.w += self.lr * y[i] * x[i]
                    self.b += self.lr * y[i]
        return self.w, self.b

训练

x = np.array(x)
y = np.array(y)
perceptron = Perceptron(2, 0.5)
w, b = perceptron.train(x, y)
print(w, b)
[-15.1   14.05] -6.0

结果展示

xl = np.linspace(0, 10, 200)
yl = (-b - w[0] * xl) / w[1]
plt.scatter(x_pos, y_pos)
plt.scatter(x_neg, y_neg)
plt.plot(xl, yl)
plt.show()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值