统计学习方法笔记,第二章感知机的python代码实现
来源:互联网 发布:linux查看任务的命令 编辑:程序博客网 时间:2024/05/29 16:49
实现的比较粗糙,代码如下:
class Perceptron: import numpy as np def __init__(self, w = 0, b = 0, lr = 1, epoch = 100): self.weight = w self.bias = b self.lr = lr #lr: learning rate self.epoch = epoch def sign(self, x): if np.dot( np.array(self.weight), x) + self.bias >= 0: return 1 else: return -1 def train(self,X, y): X_train = np.array(X) convergent = False epoch = 0 while not convergent and epoch < self.epoch: convergent = True for i in range(np.array(X_train).shape[0]): x_i = X_train[i] y_i = y[i] if y_i * self.sign(x_i) > 0: pass else: convergent = False self.weight += self.lr * y_i*x_i self.bias += self.lr * y_i epoch += 1 def test(self,X): X_test = np.array(X) y_test = np.zeros(X_test.shape[0]) for i in range(X_test.shape[0]): y_test[i] = self.sign(X_test[i]) return y_test def print_arribute(self): print("weight:",self.weight) print("bias:",self.bias)
用书上例2.1测试一下结果:
X = [[3,3],[4,3],[1,1]]y = [1,1,-1]#init the perceptron with [0,0] as weight, 0 as biasperceptron = Perceptron([0,0],0)#trainperceptron.train(X,y)#check the parametersperceptron.print_arribute()
结果如下
('weight:', array([1, 1]))('bias:', -3)
尝试一下训练集的结果
perceptron.test(X)
array([ 1., 1., -1.])
阅读全文
0 0
- 统计学习方法笔记,第二章感知机的python代码实现
- 统计学习方法笔记,第二章笔记, 感知机
- 统计学习方法第二章笔记——感知机
- 统计学习方法笔记--第二章感知机perceptron
- 《统计学习方法》学习笔记—感知机模型python实现
- 李航《统计学习方法》第二章 感知机
- 《统计学习方法》第二章 感知机
- 统计学习方法之第二章感知机
- 统计学习方法 第二章 感知机
- 《统计学习方法》-第二章-感知机
- 统计学习方法-第二章-感知机
- 统计学习方法(第二章)感知机
- 统计学习方法---感知机算法实现代码
- 统计学习方法笔记1——感知机(perceptron)的Python实现
- 统计学习方法 第2章+ 感知机python实现
- 统计学习方法感知机的实现
- 统计学习方法笔记-感知机
- 《统计学习方法》——对偶感知器的python实现
- cf 828D
- HTTP协议详解
- linux下添加定时任务
- Effective C++笔记(7)—实现
- 1031. Hello World for U (20)
- 统计学习方法笔记,第二章感知机的python代码实现
- 从今天开始 把我自己所有这几年的Android开发总结一下
- [Go]IDEA配置-IDEA
- webpack构建React应用四:使用webpack Loaders 模块加载器(二)
- Hadoop好友推荐系统-去重后的数据存入数据库
- 视频关键帧打点(视频信息提示点)的效果
- 微信小程序周报(第六期)
- 20 世纪艺术史
- 选择项目管理工具六大雷区