感知机学习算法及其Python实现

 

 

Perceptron.py

 1 from numpy import *  
 2 import operator
 3 import os
 4 
 5 
 6 
 7 # create a dataset which contains 3 samples with 2 classes  
 8 def createDataSet():  
 9     # create a matrix: each row as a sample  
10     group = array([[3,3], [4,3], [1,1]])  
11     labels = [1, 1, -1] # four samples and two classes  
12     return group, labels
13 
14 #classify using perceptron
15 def perceptronClassify(trainGroup,trainLabels):
16     global w, b
17     isFind = False  #the flag of find the best w and b
18     numSamples = trainGroup.shape[0]
19     mLenth = trainGroup.shape[1]
20     w = [0]*mLenth
21     b = 0
22     while(not isFind):
23         for i in xrange(numSamples):
24             if cal(trainGroup[i],trainLabels[i]) <= 0:
25                 print w,b
26                 update(trainGroup[i],trainLabels[i])
27                 break    #end for loop
28             elif i == numSamples-1:
29                 print w,b
30                 isFind = True   #end while loop
31 
32 
33 def cal(row,trainLabel):
34     global w, b
35     res = 0
36     for i in xrange(len(row)):
37         res += row[i] * w[i]
38     res += b
39     res *= trainLabel
40     return res
41 def update(row,trainLabel):
42     global w, b
43     for i in xrange(len(row)):
44         w[i] += trainLabel * row[i]
45     b += trainLabel

testPerceptron.py

1 import perceptron
2 g,l = perceptron.createDataSet()
3 perceptron.perceptronClassify(g,l)
View Code

 

 

DualPerceptron.py

 1 from numpy import *  
 2 import operator
 3 import os
 4 
 5 
 6 
 7 # create a dataset which contains 3 samples with 2 classes  
 8 def createDataSet():  
 9     # create a matrix: each row as a sample  
10     group = array([[3,3], [4,3], [1,1]])  
11     labels = [1, 1, -1] # four samples and two classes  
12     return group, labels
13 
14 #classify using DualPerception
15 def dualPerceptionClassify(trainGroup,trainLabels):
16     global a,b
17     isFind = False
18     numSamples = trainGroup.shape[0]
19     #mLenth = trainGroup.shape[1]
20     a = [0]*numSamples
21     b = 0
22     gMatrix = cal_gram(trainGroup)
23     while(not isFind):
24         for i in xrange(numSamples):
25             if cal(gMatrix,trainLabels,i) <= 0:
26                 cal_wb(trainGroup,trainLabels)
27                 update(i,trainLabels[i])
28                 break
29             elif i == numSamples-1:
30                 cal_wb(trainGroup,trainLabels)
31                 isFind = True
32     
33 
34 # caculate the Gram matrix
35 def cal_gram(group):
36     mLenth = group.shape[0]
37     gMatrix = zeros((mLenth,mLenth))
38     for i in xrange(mLenth):
39         for j in xrange(mLenth):
40             gMatrix[i][j] =  dot(group[i],group[j])
41     return gMatrix
42 
43 def update(i,trainLabel):
44     global a,b
45     a[i] += 1
46     b += trainLabel
47 
48 def cal(gMatrix,trainLabels,key):
49     global a,b
50     res = 0
51     for i in xrange(len(trainLabels)):
52         res += a[i]*trainLabels[i]*gMatrix[key][i]
53     res = (res + b)*trainLabels[key]
54     return res
55 
56 #caculator w and b,then print it
57 def cal_wb(group,labels):
58     global a,b
59     w=[0]*(group.shape[1])
60     h = 0
61     for i in xrange(len(labels)):
62         h +=a[i]*labels[i]
63         w +=a[i]*labels[i]*group[i]
64     print w,h

testPerceptron.py

1 import DualPerception
2 g,l = DualPerception.createDataSet()
3 DualPerception.dualPerceptionClassify(g,l)
View Code

 

 

 

 

posted on 2016-07-11 16:56  紫zzZ  阅读(5736)  评论(0编辑  收藏  举报