import numpy as np
import matplotlib.pyplot as plt
classPerceptron_orginal:def__init__(self,n=1,max_iter=10):
self.rate = n
self.max_iter = max_iter
deffit(self,X,Y):
X = np.array(X)
Y = np.array(Y)
self.feature_num =len(X[0])
self.w = np.zeros(self.feature_num)
self.b =0
self.iter=0whilebool(1-(self.predict(X)== Y).all()):for i,j inzip(X,Y):
result = self.predict(i)*j
if result >0:continueelse:
self.w += i*j*self.rate
self.b += j*self.rate
self.iter+=1if self.iter>= self.max_iter:print('cant not completely delieve the data')breakprint('training complete')passdefpredict(self,X):return np.sign(np.sum(self.w * X,-1)+ self.b)defmain():
p1 = Perceptron_orginal(0.5,20)
x =[[3,3],[4,3],[1,1],[1,2],[6,2]]
y =[1,1,-1,1,-1]
p1.fit(x,y)print(p1.predict(x))print(p1.w)print(p1.b)
positive_ = np.array(x)[np.array(y)==1]
negetive_ = np.array(x)[np.array(y)==-1]
plt.scatter([k[0]for k in positive_],[k[1]for k in positive_],c='r',label='1')
plt.scatter([k[0]for k in negetive_],[k[1]for k in negetive_],c='b',label='0')
x_ = np.arange(0,10,0.1)
y_ =-(p1.w[0]* x_ + p1.b)/ p1.w[1]
plt.plot(x_, y_)
plt.show()if __name__ =='__main__':
main()####result####################/usr/bin/python3 /Users/zhengyanzhao/PycharmProjects/tongjixuexi/perceptron_original.py
training complete
[1.1.-1.1.-1.][-2.3.5]-2.0
对偶形式实现
import numpy as np
import matplotlib.pyplot as plt
classPerceptron_dual:def__init__(self,n=1,max_iter=10):
self.max_iter = max_iter
self.n = n
deffit(self,X,Y):
X = np.array(X)
Y = np.array(Y)
sample_num =len(X)
self.b =0
self.a = np.zeros(sample_num)
self.w = np.sum((np.array(self.a)* np.array(Y))* np.array(X).T,-1)#计算Gram矩阵
self.G = np.zeros((sample_num,sample_num))for i inrange(sample_num):for j inrange(sample_num):
self.G[i,j]=X[i].dot(X[j])
self.iter=0whilebool(1-(self.predict(X)== Y).all()):for index,(i,j)inenumerate(zip(X,Y)):
result =0for m inrange(sample_num):
result_mid = self.a[m]*Y[m]*self.G[m,index]
result += result_mid
if j*(result + self.b)>0:continueelse:
self.a[index]+= self.n
self.b += j*self.n
print(self.a,self.b)
self.iter+=1if self.iter>= self.max_iter:print('cant not completely delieve the data')break
self.w = np.sum((np.array(self.a)* np.array(Y))* np.array(X).T,-1)print('training complete')passdefpredict(self,X):return np.sign(np.sum(self.w * X,-1)+ self.b)defmain():
p1 = Perceptron_dual(1,20)
x =[[3,3],[4,3],[1,1]]
y =[1,1,-1]
p1.fit(x,y)print(p1.predict(x))print(p1.w)print(p1.b)
positive_ = np.array(x)[np.array(y)==1]
negetive_ = np.array(x)[np.array(y)==-1]
plt.scatter([k[0]for k in positive_],[k[1]for k in positive_],c='r',label='1')
plt.scatter([k[0]for k in negetive_],[k[1]for k in negetive_],c='b',label='0')
x_ = np.arange(0,10,0.1)
y_ =-(p1.w[0]* x_ + p1.b)/ p1.w[1]
plt.plot(x_, y_)
plt.show()if __name__ =='__main__':
main()####result####################[1.0.0.]1[1.0.1.]0[1.0.2.]-1[1.0.3.]-2[2.0.3.]-1[2.0.4.]-2[2.0.5.]-3
training complete
[1.1.-1.][1.1.]-3
原始形式实现import numpy as npimport matplotlib.pyplot as pltclass Perceptron_orginal: def __init__(self,n=1,max_iter=10): self.rate = n self.max_iter = max_iter def fit(self,X,Y): X = np.array(X) Y = np.array(Y)