模式分类

《神经网络与机器学习》第三版,36页

 

实验代码:

# -*- coding: gb2312 -*-
import pylab as pl
import numpy as np
import random
#产生数据,r为每个月亮的半径,w为月亮的高度
#n为数目,d为圆心的垂直距离
def productData(n,r,w,d=0):
    Train_One=[]
    Train_Two=[]  
    temp=0
    while 1:
        x=random.uniform(-1*r-w/2,r+w/2)
        y=random.uniform(0,r+w/2)
        if x**2+y**2>(r-w/2)**2 and x**2+y**2<(r+w/2)**2:
            Train_One.append([x,y])
            temp+=1
        if temp==n:
            break
    temp=0
    while 1:
        x=random.uniform(-1*(w/2),2*r+w/2)
        y=random.uniform(-1*d,-1*d-r-w/2)
        if (x-r)**2+(y+d)**2>(r-w/2)**2 and (x-r)**2+(y+d)**2<(r+w/2)**2:
            Train_Two.append([x,y])
            temp+=1
        if temp==n:
            break   
    return Train_One,Train_Two
#训练
def Test(train_one,train_Two,k=1):
    w=np.ones(3)
    while 1:
        isChange=0
        for x1,x2 in train_one:
            if w[0]*x1+w[1]*x2+w[2]>0:
                pass
            else:
                isChange=1
                w=w+[k*x1,k*x2,k*1]
        for x1,x2 in train_Two:
            if w[0]*x1+w[1]*x2+w[2]<0:
                pass
            else:
                isChange=1
                w=w-[k*x1,k*x2,k*1]
        print(w)
        #如果w没有改变,则退出
        if isChange==0:
            break        
        #显示出当前的分割线
        x=[-1*r-w,2*r+w]
        if w[2]!=0:
            y=[(w[0]*x[0]+w[2])/(-1*w[1]),(w[0]*x[1]+w[2])/(-1*w[1])]
        else:
            y=[0,0]
        pl.plot(x,y,'--',c='y')
        
    return w
if __name__=="__main__":
    r=10
    w=6
    d=0.1
    Train_One,Train_Two=productData(1000,r,w,d)
    w2=Test(Train_One,Train_Two,k=0.01)
    #显示最终结果
    x=[-1*r-w,2*r+w]
    y=[(w2[0]*x[0]+w2[2])/(-1*w2[1]),(w2[0]*x[1]+w2[2])/(-1*w2[1])]
    pl.plot(x,y,c='r')
    pl.scatter([x[0] for x in Train_One],[x[1] for x in Train_One],c='g')
    pl.scatter([x[0] for x in Train_Two],[x[1] for x in Train_Two],c='b')
    pl.show()

实验结果显示:

posted @ 2014-04-15 21:36  sxcww  阅读(206)  评论(0编辑  收藏  举报