python实现感知机(perceptron)原型~

初学统计学习,用的李航的《统计学习方法》课本,感觉实在太有意思了,甚至准备读个应用统计的研究生深入一下。

随手把感知机(perceptron)用python实现了一下,用的书上的例题,代码如下:

(顺便一提,csdn插入代码真是神特么难用,动不动就是切断代码片,动不动就是多了一段html代码……)

##writen by seasonix
'''
Test document:
[3, 3] 1
[2, 2] 0
[1, 1] -1
[0, 0] -2
[3, 3] -1
[2, 2] -2
[1, 1] -3
RESULTS: at last, w is  [1, 1]
RESULYS: at last, b is  -3

'''
w = [0, 0]
b = 0
j=0
training_data = [[(3, 3), 1], [(4, 3), 1], [(1, 1), -1]]
learning_rate = 1
##training data and learning rate are gived as above.

##As follows,here is the calculated/verdicted section of the perceptrom
def cal(item):
    global w, b, j
    ver = 1
    j = 0
    while j < len(item):
        verdict = item[j][1] * (w[0] * item[j][0][0] + w[1] * item[j][0][1] + b)
        if verdict <= 0:
            ver = -1
            break
        j+=1
    return ver

##As follows,here is the updated section of the perceptron
def update(item, i):
    global w, b
    w[0] = w[0] + learning_rate * item[i][1] * item[i][0][0]
    w[1] = w[1] + learning_rate * item[i][1] * item[i][0][1]
    b = b + learning_rate * item[i][1]
    print(w,b)

##As follows,here is the main part of the perceptron.
def perceptron(item):
    global w, b
    print(w,b)
    temp=-1
    while temp!=b:
        temp=b
        if cal(item) <= 0:
            update(item, j)


##def main():
##Call the perceptron function to get the results.
perceptron(training_data)
print("RESULTS: at last, w is ",w)
print("RESULTS: at last, b is  ",b)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值