您的位置:首页 > 编程语言 > Python开发

机器学习算法——感知机(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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息