原始形式实现
import numpy as np
import matplotlib.pyplot as pltclass Perceptron_orginal:def __init__(self,n=1,max_iter=10):self.rate = nself.max_iter = max_iterdef fit(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 = 0self.iter = 0while bool(1-(self.predict(X) == Y).all()):for i,j in zip(X,Y):result = self.predict(i)*jif result >0:continueelse:self.w += i*j*self.rateself.b += j*self.rateself.iter += 1if self.iter >= self.max_iter:print('cant not completely delieve the data')breakprint('training complete')passdef predict(self,X):return np.sign(np.sum(self.w * X, -1) + self.b)def main():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()
/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 pltclass Perceptron_dual:def __init__(self,n=1,max_iter=10):self.max_iter = max_iterself.n = ndef fit(self,X,Y):X = np.array(X)Y = np.array(Y)sample_num = len(X)self.b = 0self.a = np.zeros(sample_num)self.w = np.sum((np.array(self.a) * np.array(Y)) * np.array(X).T, -1)self.G = np.zeros((sample_num,sample_num))for i in range(sample_num):for j in range(sample_num):self.G[i,j]=X[i].dot(X[j])self.iter = 0while bool(1-(self.predict(X) == Y).all()):for index,(i,j) in enumerate(zip(X,Y)):result = 0for m in range(sample_num):result_mid = self.a[m]*Y[m]*self.G[m,index]result += result_midif j*(result + self.b) >0:continueelse:self.a[index] += self.nself.b += j*self.nprint(self.a,self.b)self.iter += 1if self.iter >= self.max_iter:print('cant not completely delieve the data')breakself.w = np.sum((np.array(self.a) * np.array(Y)) * np.array(X).T, -1)print('training complete')passdef predict(self,X):return np.sign(np.sum(self.w * X, -1) + self.b)def main():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()
[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
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!