国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

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

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

瀏覽:2日期: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 一維線性回歸 內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 国产美女一区精品福利视频 | 日本高清va不卡视频在线观看 | yellow中文字幕久久网 | 久久久久久亚洲精品影院 | 亚洲精品色一区二区三区 | 亚洲国产成人久久一区www | 男人的天堂久久爱 | 亚洲国产精品成 | 日本特黄特色高清免费视频 | 成人禁在线观看午夜亚洲 | 成人高清在线观看播放 | 91香蕉国产亚洲一区二区三区 | 免费看一片| 国产亚洲91 | 男女免费观看视频 | 亚洲一区毛片 | 精品视频一二三区 | 国产三级午夜理伦三级 | 欧美视频在线观 | 一本大道香蕉大vr在线吗视频 | 亚洲欧美日韩另类精品一区二区三区 | 成人午夜毛片 | 久久韩国 | 日本一区二区三区不卡视频中文字幕 | 色综合久久久久久久 | 成人国产精品久久久免费 | 亚洲欧美一区二区三区在线播放 | 成人免费网址在线 | 国内自拍在线视频高清 | 国产成人午夜精品免费视频 | 美女扒开腿让男人桶爽免费动态图 | 91精品国产综合久久久久久 | 国产90后美女露脸在线观看 | 热久久91| 国产又色又爽黄的网站免费 | 99久久成人| 久久在线一区 | 成年女人毛片 | 农村寡妇一级毛片免费看视频 | 亚洲国产一区二区三区a毛片 | 国产一极毛片 |