使用感知机训练加法模型
时间:2022-05-06
本文章向大家介绍使用感知机训练加法模型,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
感知机此处不介绍,这里只是简单的做了一个使用感知机思路,训练一个y=a+b计算模型.
1 # -*-coding:utf-8-*-
2 '@author: xijun.gong'
3 import numpy as np
4 import random
5 import math
6
7
8 class Perceptron:
9 def __init__(self, learnRate, maxIter, bit_len):
10 """
11 :param bit_len
12 :param learnRate:
13 :param maxIter: 最大迭代次数
14 """
15 self.learmRate = learnRate;
16 self.weight = None;
17 self.maxIter = maxIter;
18 # produce map
19 self.bit_len = bit_len;
20 self.nummap = None;
21 self.initMap()
22 pass
23
24 def initMap(self):
25 maxNum = (1 << self.bit_len); # 该位数下的最大值
26 self.nummap = np.zeros((maxNum, self.bit_len), dtype=np.int); # include zero
27 for _id in xrange(maxNum):
28 for index in xrange(self.bit_len):
29 self.nummap[_id][index] = 1 & (_id >> index);
30 pass
31
32 def initWeight(self):
33 """
34 :return:
35 """
36 self.weight = np.ones(self.bit_len) / self.bit_len;
37
38 def fit(self, fds, labels):
39 """
40 :param fds: 训练样本集合
41 :param labels:
42 :return:
43 """
44 feature_nums = fds.shape[1] # 样本中的特征参数数量
45 self.initWeight()
46 for iter in xrange(self.maxIter):
47 print 'train as iter is {} '.format(iter)
48 acc_cnt = 0
49 for _ind, sample in enumerate(fds):
50 a = self.nummap[int(sample[0])];
51 b = self.nummap[int(sample[1])];
52 label_y = sum(self.weight * (a + b))
53 # 计算var_w 表示倒三角w
54 print 'the reality:{} , predict {}'.format(labels[_ind], label_y);
55 if math.fabs(labels[_ind] - label_y) <= 0.000001:
56 acc_cnt += 1;
57 continue;
58 var_w = self.learmRate * (labels[_ind] - label_y) * (a + b)
59 self.weight += var_w;
60 print 'accuary is {}'.format(acc_cnt / (len(fds) * 1.0))
61 if acc_cnt == len(fds):
62 np.save('weight.npy', {'weight': self.weight});
63 return;
64 pass
65
66 def load(self, path='weight.npy'):
67 return np.load(path)['weight']
68
69 def predict(self, fd):
70 a = self.nummap[fd[0]];
71 b = self.nummap[fd[1]];
72 return sum(self.weight * (a + b))
73
74 def predict_prod(self):
75 pass
76
77
78 if __name__ == '__main__':
79 import time
80
81 perceptron = Perceptron(learnRate=0.01, maxIter=2000, bit_len=5);
82 xa = np.arange(31);
83 xb = np.zeros(31);
84 labels = np.zeros(31)
85 for i in xrange(31):
86 xb[i] = random.randint(0, (int(time.time() + 1)) % 31)
87 labels[i] = xb[i] + xa[i]
88 perceptron.fit(np.array([xa, xb]).T, labels)
89 print 'predict is {}'.format(perceptron.predict([24, 13]))
运行结果:
train as iter is 277
the reality:0.0 , predict 0.0
the reality:16.0 , predict 16.0000005749
the reality:16.0 , predict 15.9999994995
the reality:3.0 , predict 3.00000059084
the reality:18.0 , predict 17.999999818
the reality:15.0 , predict 15.0000000195
the reality:20.0 , predict 19.9999998534
the reality:22.0 , predict 22.0000009642
the reality:10.0 , predict 9.99999911021
the reality:22.0 , predict 21.9999996143
the reality:23.0 , predict 22.9999990943
the reality:17.0 , predict 17.0000000549
the reality:25.0 , predict 24.9999994128
the reality:18.0 , predict 18.0000008934
the reality:20.0 , predict 19.9999998534
the reality:15.0 , predict 15.0000000195
the reality:27.0 , predict 26.999999038
the reality:31.0 , predict 30.9999993919
the reality:25.0 , predict 25.0000003525
the reality:21.0 , predict 20.9999999986
the reality:35.0 , predict 34.9999997457
the reality:29.0 , predict 28.9999993564
the reality:39.0 , predict 38.9999996894
the reality:26.0 , predict 26.0000009079
the reality:31.0 , predict 30.9999993919
the reality:25.0 , predict 24.9999990026
the reality:33.0 , predict 32.9999994273
the reality:32.0 , predict 31.9999999473
the reality:32.0 , predict 31.9999991549
the reality:34.0 , predict 34.0000002657
the reality:33.0 , predict 32.9999994273
accuary is 1.0
predict is 36.9999984312
- 未来几年苹果公司很可能“不务正业”,进军汽车无人驾驶界
- ExtJs学习笔记(15)_fit布局
- JavaScript之call()和apply()方法详解
- Javascript快速入门(下篇)
- JavaScript引用类型之Array数组的栈方法与队列方法
- ExtJs学习笔记(14)_Column布局
- 高级盲注—floor,rand,group by报错注入
- 刷脸注册、试装、支付……仅靠一张脸就能买买买的时尚店开业了
- JavaScript引用类型之Array数组之强大的splice()方法
- Linux快速入门03-系统管理
- JavaScript引用类型之Array数组的concat()和push()方法的区别
- JavaScript引用类型之Array数组的排序方法
- Linux快速入门02-文件系统管理
- JavaScript引用类型之Array数组的toString()和valueof()方法的区别
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 什么是前缀树--打开了我的新思路
- 一个案例搞懂原码、反码、补码,不懂得请看过来
- 人人都在用,但你却不知道它背后发生了什么——浏览器的工作原理:浏览器幕后揭秘
- 这有一把钥匙,打开MySQL死锁问题!
- 普通人如何全面了解大数据的特点,意义和发展前景
- 面试官问我Linux下常见网络命令
- 最全常用User-Agent
- 聊到JVM(还怕面试官问JVM吗?)
- Android.location.Address类方法获取GPS定位信息
- Python二叉树详解笔记
- 《剑指offer》第七天:二叉树的下一个结点
- 后台登录微信并定时发送消息,消息包括农历、阴历、天气;自动监测是否断线,支持邮箱发送二维码登录;适合于挂在服务器上运行
- 《剑指offer》第八天:二叉树的下一个结点
- 基于python和OpenCV构建智能停车系统
- nvm管理工具