Code | 线性分类器:批量感知算法+Ho_Kashyap算法(二分类)+MSE多类扩展

1. 数据

2.

import matplotlib.pyplot as plt
import numpy as np

x = np.loadtxt('data.txt')
b = np.ones(40)
y = np.insert(x, 0, b, 1)  # 增广

class BatchPerception():
    def __init__(self, w1, w2, y):
        self.w1 = w1
        self.w2 = w2
        self.a = np.zeros(3)
        self.count = 0
        self.lr = 1
        self.y = y

    def preprocess(self):
        y_temp = self.y.copy()
        y_w1 = y_temp[(self.w1 - 1) * 10:self.w1 * 10, 0:3]
        y_w2 = -1 * y_temp[(self.w2 - 1) * 10:self.w2 * 10, 0:3] # 规范化
        y_w = np.concatenate((y_w1, y_w2), axis=0)
        return y_w

    def train(self):
        y_w = self.preprocess()
        for j in range(1000):
            Y = []

            for i in range(20):
                if np.inner(self.a, y_w[i]) <= 0:
                    Y.append(y_w[i])
#                    print(np.inner(self.a, y[i][0:3]))
            if len(Y) == 0:
                print(self.w1, '和', self.w2, self.a, self.count)
                break
            Y_sum = np.sum(Y, axis=0)
            self.a = self.a + self.lr * Y_sum
            self.count += 1

    def visualization(self
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值