日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

统计学习方法第二章作业:感知机模型原始形式与对偶形式代码实现

發(fā)布時(shí)間:2025/3/8 编程问答 9 豆豆
生活随笔 收集整理的這篇文章主要介紹了 统计学习方法第二章作业:感知机模型原始形式与对偶形式代码实现 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

原始形式實(shí)現(xiàn)

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

對偶形式實(shí)現(xiàn)

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)#計(jì)算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

總結(jié)

以上是生活随笔為你收集整理的统计学习方法第二章作业:感知机模型原始形式与对偶形式代码实现的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。