感知机学习算法的简单实现(Python)

算法(原始形式,出自李航博士的统计学习方法)
输入:T={(x1,y1),(x2,y2)…(xN,yN)}(其中xi∈X=Rn,yi∈Y={-1,+1},i=1,2…N,学习速率为η)
输出:w, b;感知机模型f(x)=sign(w·x+b)
1. 初始化w0,b0
2. 在训练数据集中选取(xi, yi)
3. 如果yi(w xi+b)≤0
w = w + ηyixi
b = b + ηyi
4. 转至2

例题2.1

对于训练数据集,其中正例点是x1=(3,3)T,x2=(4,3)T,负例点为x3=(1,1)T,用感知机学习算法的原始形式求感知机模型f(x)=w·x+b。这里w=(w(1),w(2))T,x=(x(1),x(2))T
简单的Python实现:

import numpy as np
training_set = [[3,3],[4,3],[1,1]]
Y = [1,1,-1]
w = [0,0]
b = 0
n = 1
#check给出w,b,找到一个此时的误分点,如果没有,说明算法收敛
def check(w,b):
    C = []
    for e in training_set:
        x1 = np.array(e).reshape((-1,1))
        w1 = np.array(w)
        c = np.dot(w1,x1)
        y = Y[training_set.index(e)]
        if y*(c+b)<=0:
            C.append(e[0])
            C.append(e[1])
            break
        else:
            continue
    return C
def update(C,w,b):
    D = []
    y = Y[training_set.index(C)]
    x = np.array(C)
    D.append(w + n*y*x) 
    D.append(b + n*y)
    return D

while True:
    C = check(w, b)
    if len(C)==0:
        break
    else:
        w = update(C,w,b)[0]
        b = update(C,w,b)[1]
print '算法已经收敛'    
print 'y = sign(%d*x1 + %d*x2 + %d)'%(w[0],w[1],b)

输出:

算法已经收敛
y = sign(1*x1 + 1*x2 + -3)

算法的对偶形式,见书P34,例2.2实现代码如下:

import numpy as np
training_set = [[3,3],[4,3],[1,1]]
Y = [1,1,-1]
a = [0,0,0]
b = 0
n = 1
N = 3
def cal(training_set):
    x11 = np.dot(np.array(training_set[0]),np.array(training_set[0]).reshape(-1,1))
    x22 = np.dot(np.array(training_set[1]),np.array(training_set[1]).reshape(-1,1))
    x33 = np.dot(np.array(training_set[2]),np.array(training_set[2]).reshape(-1,1))
    x12 = x21 = np.dot(np.array(training_set[0]),np.array(training_set[1]).reshape(-1,1))
    x13 = x31 = np.dot(np.array(training_set[0]),np.array(training_set[2]).reshape(-1,1))
    x23 = x32 = np.dot(np.array(training_set[1]),np.array(training_set[2]).reshape(-1,1))
    return [[x11,x12,x13],[x21,x22,x23],[x31,x32,x33]]                 
def check(a,b):
    C = []
    X = cal(training_set)
    for i in range(N):
        y = Y[i]
        L = 0
        for j in range(N):
            L = L + a[j]*Y[j]*X[j][i]
        if y*(L + b) <= 0:
            C = training_set[i]
            break
        else:
            continue
    return C
def update(C,a,b):
    D = []
    y = Y[training_set.index(C)]
    a[training_set.index(C)] = a[(training_set.index(C)] + n
    D.append(a)
    D.append(b + n*y)
    return D
while True:
    C = check(a, b)
    if len(C)==0:
        break
    else:
        a = update(C,a,b)[0]
        b = update(C,a,b)[1]
print u'算法已收敛'    
print a,b                    

输出:

算法已收敛
[6, 0, 16] -5

你可能感兴趣的:(算法实现)