机器学习算法——感知机(Python源码)
2016-04-17 15:42
495 查看
#*-encoding:utf-8-* ######################################################################################################## # Summary: 感知机模型 # Author: Kurt # Date: 2016/3/23 ######################################################################################################## #------------------------------------------------------------------------------------------------------- # 装作有数据的函数 def createData(): data = [[3, 3, 1], [4, 3, 1], [1, 1, -1]] return data #------------------------------------------------------------------------------------------------------- # 更新权重 def update(weight, tuple, step): weight[0] += step * tuple[-1] for i in range(len(tuple) -1): weight[i+1] += step * tuple[i] * tuple[-1] return weight #------------------------------------------------------------------------------------------------------- # 返回权重与样本点的向量点积 def calculate(weight, tuple): sum = weight[0] for i in range(len(tuple) -1): sum += weight[i+1] * tuple[i] sum *= tuple[-1] print sum return sum #------------------------------------------------------------------------------------------------------- # 感知机模型 def percetron(dataSet, step = 1): w = [] # 权重向量,里面包括了偏倚b isNotDone = True # 初始化权重向量 for i in range(len(dataSet[0])): w.append(0) while(isNotDone): print "\n\nScan the data again" # 遍历每一个样本点 for tuple in dataSet: # 如果分类错误 if(calculate(w, tuple) <= 0): print "Error point: " print tuple isNotDone = True update(w, tuple, step) print "----->>>New weight is ", w break else: isNotDone = False return w
相关文章推荐
- Python动态类型的学习---引用的理解
- 从源码安装Mysql/Percona 5.5
- Python3写爬虫(四)多线程实现数据爬取
- 垃圾邮件过滤器 python简单实现
- 下载并遍历 names.txt 文件,输出长度最长的回文人名。
- install and upgrade scrapy
- Scrapy的架构介绍
- Centos6 编译安装Python
- 使用Python生成Excel格式的图片
- 让Python文件也可以当bat文件运行
- [Python]推算数独
- Python中zip()函数用法举例
- Python中map()函数浅析
- Python将excel导入到mysql中
- Python在CAM软件Genesis2000中的应用
- 使用Shiboken为C++和Qt库创建Python绑定
- FREEBASIC 编译可被python调用的dll函数示例