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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python线性加权模型_局部加权之线性回归(1) - Python实现

發布時間:2023/12/3 python 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python线性加权模型_局部加权之线性回归(1) - Python实现 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1 #局部加權線性回歸

2 #交叉驗證計算泛化誤差最小點

3

4

5 importnumpy6 from matplotlib importpyplot as plt7

8

9 #待擬合不含噪聲之目標函數

10 deforiFunc(x):11 y = numpy.exp(-x) * numpy.sin(10*x)12 returny13 #待擬合包含噪聲之目標函數

14 def traFunc(x, sigma=0.03):15 y = oriFunc(x) +numpy.random.normal(0, sigma, numpy.array(x).size)16 returny17

18

19 #局部加權線性回歸之實現

20 classLW(object):21

22 def __init__(self, xBound=(0, 3), number=500, tauBound=(0.001, 100), epsilon=1.e-3):23 self.__xBound = xBound #采樣邊界

24 self.__number = number #采樣數目

25 self.__tauBound = tauBound #tau之搜索邊界

26 self.__epsilon = epsilon #tau之搜索精度

27

28

29 defget_data(self):30 '''

31 根據目標函數生成待擬合數據32 '''

33 X = numpy.linspace(*self.__xBound, self.__number)34 oriY_ = oriFunc(X) #不含誤差之響應

35 traY_ = traFunc(X) #包含誤差之響應

36

37 self.X = numpy.vstack((X.reshape((1, -1)), numpy.ones((1, X.shape[0]))))38 self.oriY_ = oriY_.reshape((-1, 1))39 self.traY_ = traY_.reshape((-1, 1))40

41 returnself.X, self.oriY_, self.traY_42

43

44 def lw_fitting(self, tau=None):45 if not hasattr(self, "X"):46 self.get_data()47 if tau isNone:48 if hasattr(self, "bestTau"):49 tau =self.bestTau50 else:51 tau =self.get_tau()52

53 xList, yList =list(), list()54 for val in numpy.linspace(*self.__xBound, self.__number * 5):55 x = numpy.array([[val], [1]])56 theta = self.__fitting(x, self.X, self.traY_, tau)57 y =numpy.matmul(theta.T, x)58 xList.append(x[0, 0])59 yList.append(y[0, 0])60

61 resiList = list() #殘差計算

62 for idx in range(self.__number):63 x = self.X[:, idx:idx+1]64 theta = self.__fitting(x, self.X, self.traY_, tau)65 y =numpy.matmul(theta.T, x)66 resiList.append(self.traY_[idx, 0] -y[0, 0])67

68 returnxList, yList, self.X[0, :].tolist(), resiList69

70

71 defshow(self):72 '''

73 繪圖展示整體擬合情況74 '''

75 xList, yList, sampleXList, sampleResiList =self.lw_fitting()76 y2List =oriFunc(numpy.array(xList))77 fig = plt.figure(figsize=(8, 14))78 ax1 = plt.subplot(2, 1, 1)79 ax2 = plt.subplot(2, 1, 2)80

81 ax1.scatter(self.X[0, :], self.traY_[:, 0], c="green", alpha=0.7, label="samples with noise")82 ax1.plot(xList, y2List, c="red", lw=4, alpha=0.7, label="standard curve")83 ax1.plot(xList, yList, c="black", lw=2, linestyle="--", label="fitting curve")84 ax1.set(xlabel="$x$", ylabel="$y$")85 ax1.legend()86

87 ax2.scatter(sampleXList, sampleResiList, c="blue", s=10)88 ax2.set(xlabel="$x$", ylabel="$\epsilon$", title="residual distribution")89

90 plt.show()91 plt.close()92 fig.tight_layout()93 fig.savefig("lw.png", dpi=300)94

95

96 def __fitting(self, x, X, Y_, tau, epsilon=1.e-9):97 tmpX = X[0:1, :]98 tmpW = (-(tmpX - x[0, 0]) ** 2 / tau ** 2 / 2).reshape(-1)99 W =numpy.diag(numpy.exp(tmpW))100

101 item1 =numpy.matmul(numpy.matmul(X, W), X.T)102 item2 = numpy.linalg.inv(item1 + epsilon *numpy.identity(item1.shape[0]))103 item3 =numpy.matmul(numpy.matmul(X, W), Y_)104

105 theta =numpy.matmul(item2, item3)106

107 returntheta108

109

110 defget_tau(self):111 '''

112 交叉驗證返回最優tau113 采用黃金分割法計算最優tau114 '''

115 if not hasattr(self, "X"):116 self.get_data()117

118 lowerBound, upperBound = self.__tauBound

119 lowerTau = self.__calc_lowerTau(lowerBound, upperBound)120 upperTau = self.__calc_upperTau(lowerBound, upperBound)121 lowerErr = self.__calc_generalErr(self.X, self.traY_, lowerTau)122 upperErr = self.__calc_generalErr(self.X, self.traY_, upperTau)123

124 while (upperTau - lowerTau) > self.__epsilon:125 if lowerErr >upperErr:126 lowerBound =lowerTau127 lowerTau =upperTau128 lowerErr =upperErr129 upperTau = self.__calc_upperTau(lowerBound, upperBound)130 upperErr = self.__calc_generalErr(self.X, self.traY_, upperTau)131 else:132 upperBound =upperTau133 upperTau =lowerTau134 upperErr =lowerErr135 lowerTau = self.__calc_lowerTau(lowerBound, upperBound)136 lowerErr = self.__calc_generalErr(self.X, self.traY_, lowerTau)137

138 self.bestTau = (upperTau + lowerTau) / 2

139 returnself.bestTau140

141

142 def __calc_generalErr(self, X, Y_, tau):143 generalErr =0144

145 for idx in range(X.shape[1]):146 tmpx = X[:, idx:idx+1]147 tmpy_ = Y_[idx:idx+1, :]148 tmpX = numpy.hstack((X[:, 0:idx], X[:, idx+1:]))149 tmpY_ = numpy.vstack((Y_[0:idx, :], Y_[idx+1:, :]))150

151 theta = self.__fitting(tmpx, tmpX, tmpY_, tau)152 tmpy =numpy.matmul(theta.T, tmpx)153 generalErr += (tmpy_[0, 0] - tmpy[0, 0]) ** 2

154

155 returngeneralErr156

157

158 def __calc_lowerTau(self, lowerBound, upperBound):159 delta = upperBound -lowerBound160 lowerTau = upperBound - delta * 0.618

161 returnlowerTau162

163

164 def __calc_upperTau(self, lowerBound, upperBound):165 delta = upperBound -lowerBound166 upperTau = lowerBound + delta * 0.618

167 returnupperTau168

169

170

171

172 if __name__ == '__main__':173 obj =LW()174 obj.show()

總結

以上是生活随笔為你收集整理的python线性加权模型_局部加权之线性回归(1) - Python实现的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。