单层感知机

在这里插入图片描述
在这里插入图片描述
注意:上面的损失函数用的是平方差损失函数。
以下是代码实现部分:

import numpy as np
import matplotlib.pyplot as plt

x = np.array([[1, 3, 3],
              [1, 4, 3],
              [1, 1, 1], 
              [1, 0, 2]])
y = np.array([[1],[1], [-1], [-1]])
w = (np.random.random((3,1))-0.5) * 2 # 把初始换参数定位在-1到1之间

lr = 0.11
output = 0

def updata(x, w, y):
    print()
    output = np.sign(np.dot(x, w))
    w_grad = lr * (x.T.dot(y-output)) / int(x.shape[0])
    w = w + w_grad
    return w
for i in range(100):
    print(i)
    w = updata(x, w, y)
    print(np.sign(np.dot(x,w)))
    if (np.sign(np.dot(x, w)) ==  y).all():
        print("Finish")
        break
k = -w[1] / w[2]
d = -w[0] / w[2]
xdata = (0, 5)
plt.figure()
plt.plot(xdata, xdata*k+d, 'r')
plt.scatter(x[:2, 1:2],x[:2, 2:3], c='b')
plt.scatter(x[2:4, 1:2],x[2:4, 2:3], c='r')
plt.legend()

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值