统计学习方法第二章作业:感知机模型原始形式与对偶形式代码实现
生活随笔
收集整理的這篇文章主要介紹了
统计学习方法第二章作业:感知机模型原始形式与对偶形式代码实现
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
原始形式實現
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()####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 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)#計算Gram矩陣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()####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總結
以上是生活随笔為你收集整理的统计学习方法第二章作业:感知机模型原始形式与对偶形式代码实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Tensorflow2.0 + Tra
- 下一篇: 统计学习方法第三章作业:一般k邻近、平衡