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

来源:互联网 发布:王健林 鲁豫 知乎 编辑:程序博客网 时间:2024/05/27 20:50

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

随手把感知机(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] -3RESULTS: at last, w is  [1, 1]RESULYS: at last, b is  -3'''w = [0, 0]b = 0j=0training_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 perceptromdef 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 perceptrondef 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)


0 1
原创粉丝点击