初学统计学习,用的李航的《统计学习方法》课本,感觉实在太有意思了,甚至准备读个应用统计的研究生深入一下。
随手把感知机(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)