感知机导论

感知机导论_第1张图片

作者:金良([email protected]) csdn博客:http://blog.csdn.net/u012176591


资源链接见http://download.csdn.net/detail/u012176591/8234033

感知机导论_第2张图片


感知机导论_第3张图片


感知机导论_第4张图片


感知机导论_第5张图片


感知机导论_第6张图片


感知机导论_第7张图片


感知机导论_第8张图片


感知机导论_第9张图片

感知机导论_第10张图片

啊啊

参考文献:

  • 此模型有个定理的英文证明可以学一下
    http://www.cs.columbia.edu/~mcollins/courses/6998-2012/notes/perc.converge.pdf
  • 统计学习方法,李航
    此模型有两种形式,分别是原始形式和对偶形式,分析一下二者之间的转换前提和过程。
  • 《统计学习方法》读书笔记——感知机
    http://www.cnblogs.com/OldPanda/archive/2013/04/12/3017100.html 
    末尾有几个链接

相关代码:

例1代码:

import numpy
import random
import matplotlib.pyplot as pyplot
def myplot(x,y,omega,b,iter):
    for i in range(x.shape[0]):
        if y[i]==-1:
            pyplot.plot(x[i][0],x[i][1],'mo')
        else:
            pyplot.plot(x[i][0],x[i][1],'k*')
    if (omega[1]==0.0)&(omega[0]==0.0):
        return
    elif omega[1]==0:
        xline=[-b/omega[0]]*100
        yline =numpy.linspace(0,5,100)
    elif omega[0]==0:
        xline =numpy.linspace(0,5,100)
        yline=[-b/omega[1]]*100
    else:
        xline=numpy.linspace(0,5,100)
        yline=(-b-omega[0]*xline)/omega[1]
    pyplot.plot(xline,yline,'b-')
    pyplot.xlim(0,4)
    pyplot.ylim(0,4)
    pyplot.xlabel('x(1)')
    pyplot.ylabel('x(2)')
    pyplot.title('The perception example')
    pyplot.savefig('./pic/perception1/'+str(iter)+'.jpg');
    pyplot.close('all')
x=numpy.array([[0.5,1.9],[2,3],[2,2],[1,1],[1,2],[2,0.5],[3,2],[1.5,0.6],[3,1.5],[2.5,1]])
y=numpy.array([-1,1,1,-1,1,-1,1,-1,1,1])
omega=numpy.array([0,0])
b=0
eta=0.03145
errorset=[]
for i in range(x.shape[0]):
    if((x[i].dot(omega)+b)*y[i]<=0):
        errorset.append(i)
iter=0        
while len(errorset)>0:
    errornum=len(errorset)
    num=random.randrange(0,errornum)
    loc=errorset[num]
    omega =omega+eta*y[loc]*x[loc]
    b =b+eta*y[loc]
    print eta*y[loc]*x[loc],y[loc],x[loc],omega,b
    myplot(x,y,omega,b,iter)
    iter+=1
    errorset=[]
    for i in range(x.shape[0]):
        if((x[i].dot(omega)+b)*y[i]<=0):
            errorset.append(i)
        
    

例2的源码

# -*- coding: cp936 -*-
import numpy
import random

import matplotlib.pyplot as pyplot



def add(x):
    temp=[]
    for i in range(x.shape[0]):
        a=x[i].tolist() 
        a.append(1)     #append插入的是整个对象,破坏了原来的结构,而不返回对象
        temp.append(a)
    x=numpy.array(temp)
    return x
def Gram(x):
    G=numpy.zeros([x.shape[0],x.shape[0]])
    for i in range(x.shape[0]):
        for j in range(i+1):
            G[i][j]=G[j][i]=x[i].dot(x[j])
    return G       

def Main():
    x=numpy.array([[3,3],[4,3],[1,1],[2,1]])
    y=numpy.array([1,1,-1,-1])
    x=add(x)
    G=Gram(x)
    alpha=numpy.zeros([1,x.shape[0]])
    eta=0.31415
    indic=alpha*y.dot(G)*y
    errorset=[]
    for i in range(indic.shape[1]):
        if indic[0][i]<=0:
            errorset.append(i)
    while len(errorset)>0:
        num=random.randrange(0,len(errorset))
        i=errorset[num]
        alpha=alpha+eta*y[i]*y*G[i]
        print alpha
        indic=alpha*y.dot(G)*y
        errorset=[]
        for i in range(indic.shape[1]):
            if indic[0][i]<=0:
                errorset.append(i)          
    print (alpha*y).dot(x)# 要用括号括起来
Main()
 

 

本博文的TeX源码https://code.csdn.net/snippets/610266/master/%E6%84%9F%E7%9F%A5%E6%9C%BA%E5%AF%BC%E8%AE%BA.tex/raw



你可能感兴趣的:(感知机,Perceptron,对偶形式)