久久福利_99r_国产日韩在线视频_直接看av的网站_中文欧美日韩_久久一

您的位置:首頁技術文章
文章詳情頁

python 還原梯度下降算法實現一維線性回歸

瀏覽:3日期:2022-07-07 15:00:22

首先我們看公式:

python 還原梯度下降算法實現一維線性回歸

這個是要擬合的函數

然后我們求出它的損失函數, 注意:這里的n和m均為數據集的長度,寫的時候忘了

python 還原梯度下降算法實現一維線性回歸

注意,前面的theta0-theta1x是實際值,后面的y是期望值接著我們求出損失函數的偏導數:

python 還原梯度下降算法實現一維線性回歸

最終,梯度下降的算法:

python 還原梯度下降算法實現一維線性回歸

學習率一般小于1,當損失函數是0時,我們輸出theta0和theta1.接下來上代碼!

class LinearRegression(): def __init__(self, data, theta0, theta1, learning_rate): self.data = data self.theta0 = theta0 self.theta1 = theta1 self.learning_rate = learning_rate self.length = len(data) # hypothesis def h_theta(self, x): return self.theta0 + self.theta1 * x # cost function def J(self): temp = 0 for i in range(self.length): temp += pow(self.h_theta(self.data[i][0]) - self.data[i][1], 2) return 1 / (2 * self.m) * temp # partial derivative def pd_theta0_J(self): temp = 0 for i in range(self.length): temp += self.h_theta(self.data[i][0]) - self.data[i][1] return 1 / self.m * temp def pd_theta1_J(self): temp = 0 for i in range(self.length): temp += (self.h_theta(data[i][0]) - self.data[i][1]) * self.data[i][0] return 1 / self.m * temp # gradient descent def gd(self): min_cost = 0.00001 round = 1 max_round = 10000 while min_cost < abs(self.J()) and round <= max_round: self.theta0 = self.theta0 - self.learning_rate * self.pd_theta0_J() self.theta1 = self.theta1 - self.learning_rate * self.pd_theta1_J() print(’round’, round, ’:t theta0=%.16f’ % self.theta0, ’t theta1=%.16f’ % self.theta1) round += 1 return self.theta0, self.theta1def main():data = [[1, 2], [2, 5], [4, 8], [5, 9], [8, 15]] # 這里換成你想擬合的數[x, y] # plot scatter x = [] y = [] for i in range(len(data)): x.append(data[i][0]) y.append(data[i][1]) plt.scatter(x, y) # gradient descent linear_regression = LinearRegression(data, theta0, theta1, learning_rate) theta0, theta1 = linear_regression.gd() # plot returned linear x = np.arange(0, 10, 0.01) y = theta0 + theta1 * x plt.plot(x, y) plt.show()

到此這篇關于python 還原梯度下降算法實現一維線性回歸 的文章就介紹到這了,更多相關python 一維線性回歸 內容請搜索好吧啦網以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 午夜精品久久久久久久 | 99影视 | 欧美国产在线视频 | 国产免费拔擦拔擦8x高清在线人 | 欧美一级h | www.国产91 | 久久久久高清 | 中文字幕国产区 | 亚洲成成品网站 | 一级a性色生活片久久毛片明星 | 噜噜噜在线视频 | 最新午夜综合福利视频 | 国产精品久久久久一区二区三区 | 北条麻妃一区二区在线 | 大香一网| 精品国产乱码久久久久久闺蜜 | 国产精品一区二区三 | 国产精品成人在线观看 | 国产高清在线精品一区二区三区 | 91免费观看视频 | 色精品 | 91av原创| www国产成人免费观看视频,深夜成人网 | 偷拍自拍网站 | 日韩免费高清在线 | 国产一区二区三区在线看 | 久久久久久久久久久九 | 中文字幕精品一区久久久久 | 一级片免费视频 | a毛片国产 | 欧美大片一区二区 | 日韩福利片 | 一级毛片免费播放 | 欧美一区久久 | 国产精品99久久久久久久vr | 精品日韩视频 | 在线观看你懂的网站 | 欧美日本一区 | 国产一区 欧美 | 黄色一级免费大片 | 亚洲 欧美 日韩 丝袜 另类 |