欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python使用梯度下降算法實(shí)現(xiàn)一個(gè)多線性回歸

 更新時(shí)間:2020年03月24日 14:37:25   作者:禿鷲紅發(fā)夜魔王  
這篇文章主要為大家詳細(xì)介紹了python使用梯度下降算法實(shí)現(xiàn)一個(gè)多線性回歸,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

python使用梯度下降算法實(shí)現(xiàn)一個(gè)多線性回歸,供大家參考,具體內(nèi)容如下

圖示:

import pandas as pd
import matplotlib.pylab as plt
import numpy as np
# Read data from csv
pga = pd.read_csv("D:\python3\data\Test.csv")
# Normalize the data 歸一化值 (x - mean) / (std)
pga.AT = (pga.AT - pga.AT.mean()) / pga.AT.std()
pga.V = (pga.V - pga.V.mean()) / pga.V.std()
pga.AP = (pga.AP - pga.AP.mean()) / pga.AP.std()
pga.RH = (pga.RH - pga.RH.mean()) / pga.RH.std()
pga.PE = (pga.PE - pga.PE.mean()) / pga.PE.std()


def cost(theta0, theta1, theta2, theta3, theta4, x1, x2, x3, x4, y):
 # Initialize cost
 J = 0
 # The number of observations
 m = len(x1)
 # Loop through each observation
 # 通過(guò)每次觀察進(jìn)行循環(huán)
 for i in range(m):
 # Compute the hypothesis
 # 計(jì)算假設(shè)
 h=theta0+x1[i]*theta1+x2[i]*theta2+x3[i]*theta3+x4[i]*theta4
 # Add to cost
 J += (h - y[i])**2
 # Average and normalize cost
 J /= (2*m)
 return J
# The cost for theta0=0 and theta1=1


def partial_cost_theta4(theta0,theta1,theta2,theta3,theta4,x1,x2,x3,x4,y):
 h = theta0 + x1 * theta1 + x2 * theta2 + x3 * theta3 + x4 * theta4
 diff = (h - y) * x4
 partial = diff.sum() / (x2.shape[0])
 return partial


def partial_cost_theta3(theta0,theta1,theta2,theta3,theta4,x1,x2,x3,x4,y):
 h = theta0 + x1 * theta1 + x2 * theta2 + x3 * theta3 + x4 * theta4
 diff = (h - y) * x3
 partial = diff.sum() / (x2.shape[0])
 return partial


def partial_cost_theta2(theta0,theta1,theta2,theta3,theta4,x1,x2,x3,x4,y):
 h = theta0 + x1 * theta1 + x2 * theta2 + x3 * theta3 + x4 * theta4
 diff = (h - y) * x2
 partial = diff.sum() / (x2.shape[0])
 return partial


def partial_cost_theta1(theta0,theta1,theta2,theta3,theta4,x1,x2,x3,x4,y):
 h = theta0 + x1 * theta1 + x2 * theta2 + x3 * theta3 + x4 * theta4
 diff = (h - y) * x1
 partial = diff.sum() / (x2.shape[0])
 return partial

# 對(duì)theta0 進(jìn)行求導(dǎo)
# Partial derivative of cost in terms of theta0


def partial_cost_theta0(theta0, theta1, theta2, theta3, theta4, x1, x2, x3, x4, y):
 h = theta0 + x1 * theta1 + x2 * theta2 + x3 * theta3 + x4 * theta4
 diff = (h - y)
 partial = diff.sum() / (x2.shape[0])
 return partial


def gradient_descent(x1,x2,x3,x4,y, alpha=0.1, theta0=0, theta1=0,theta2=0,theta3=0,theta4=0):
 max_epochs = 1000 # Maximum number of iterations 最大迭代次數(shù)
 counter = 0 # Intialize a counter 當(dāng)前第幾次
 c = cost(theta0, theta1, theta2, theta3, theta4, x1, x2, x3, x4, y) ## Initial cost 當(dāng)前代價(jià)函數(shù)
 costs = [c] # Lets store each update 每次損失值都記錄下來(lái)
 # Set a convergence threshold to find where the cost function in minimized
 # When the difference between the previous cost and current cost
 # is less than this value we will say the parameters converged
 # 設(shè)置一個(gè)收斂的閾值 (兩次迭代目標(biāo)函數(shù)值相差沒(méi)有相差多少,就可以停止了)
 convergence_thres = 0.000001
 cprev = c + 10
 theta0s = [theta0]
 theta1s = [theta1]
 theta2s = [theta2]
 theta3s = [theta3]
 theta4s = [theta4]
 # When the costs converge or we hit a large number of iterations will we stop updating
 # 兩次間隔迭代目標(biāo)函數(shù)值相差沒(méi)有相差多少(說(shuō)明可以停止了)
 while (np.abs(cprev - c) > convergence_thres) and (counter < max_epochs):
 cprev = c
 # Alpha times the partial deriviative is our updated
 # 先求導(dǎo), 導(dǎo)數(shù)相當(dāng)于步長(zhǎng)
 update0 = alpha * partial_cost_theta0(theta0, theta1, theta2, theta3, theta4, x1, x2, x3, x4, y)
 update1 = alpha * partial_cost_theta1(theta0, theta1, theta2, theta3, theta4, x1, x2, x3, x4, y)
 update2 = alpha * partial_cost_theta2(theta0, theta1, theta2, theta3, theta4, x1, x2, x3, x4, y)
 update3 = alpha * partial_cost_theta3(theta0, theta1, theta2, theta3, theta4, x1, x2, x3, x4, y)
 update4 = alpha * partial_cost_theta4(theta0, theta1, theta2, theta3, theta4, x1, x2, x3, x4, y)
 # Update theta0 and theta1 at the same time
 # We want to compute the slopes at the same set of hypothesised parameters
 #  so we update after finding the partial derivatives
 # -= 梯度下降,+=梯度上升
 theta0 -= update0
 theta1 -= update1
 theta2 -= update2
 theta3 -= update3
 theta4 -= update4

 # Store thetas
 theta0s.append(theta0)
 theta1s.append(theta1)
 theta2s.append(theta2)
 theta3s.append(theta3)
 theta4s.append(theta4)

 # Compute the new cost
 # 當(dāng)前迭代之后,參數(shù)發(fā)生更新
 c = cost(theta0, theta1, theta2, theta3, theta4, x1, x2, x3, x4, y)

 # Store updates,可以進(jìn)行保存當(dāng)前代價(jià)值
 costs.append(c)
 counter += 1 # Count
 # 將當(dāng)前的theta0, theta1, costs值都返回去
 #return {'theta0': theta0, 'theta1': theta1, 'theta2': theta2, 'theta3': theta3, 'theta4': theta4, "costs": costs}
 return {'costs':costs}

print("costs =", gradient_descent(pga.AT, pga.V,pga.AP,pga.RH,pga.PE)['costs'])
descend = gradient_descent(pga.AT, pga.V,pga.AP,pga.RH,pga.PE, alpha=.01)
plt.scatter(range(len(descend["costs"])), descend["costs"])
plt.show()

損失函數(shù)隨迭代次數(shù)變換圖:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 詳解Python中的自定義密碼驗(yàn)證

    詳解Python中的自定義密碼驗(yàn)證

    這篇文章主要為大家介紹了如何實(shí)現(xiàn)在Python中的自定義密碼驗(yàn)證,并對(duì)密碼驗(yàn)證功能進(jìn)行單元測(cè)試。文中的示例代碼講解詳細(xì),需要的可以參考一下
    2022-02-02
  • python對(duì)文檔中元素刪除,替換操作

    python對(duì)文檔中元素刪除,替換操作

    這篇文章主要介紹了python對(duì)文檔中元素刪除,替換操作,pthon更換文檔中某元素、python改變或者刪除txt文檔中某一列元素,下文具體代碼實(shí)現(xiàn)需要的小伙伴可以參考一下
    2022-04-04
  • 基于Python輕松制作一個(gè)股票K線圖網(wǎng)站

    基于Python輕松制作一個(gè)股票K線圖網(wǎng)站

    在當(dāng)今這個(gè)人手一個(gè)?Web?服務(wù)的年代,GUI?程序還是沒(méi)有?Web?服務(wù)來(lái)的香啊。所以本文將用Python制作一個(gè)簡(jiǎn)單的股票K線圖網(wǎng)站,感興趣的可以了解一下
    2022-09-09
  • Python實(shí)現(xiàn)http接口自動(dòng)化測(cè)試的示例代碼

    Python實(shí)現(xiàn)http接口自動(dòng)化測(cè)試的示例代碼

    這篇文章主要介紹了Python實(shí)現(xiàn)http接口自動(dòng)化測(cè)試的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-10-10
  • Python實(shí)現(xiàn)服務(wù)端渲染SSR的示例代碼

    Python實(shí)現(xiàn)服務(wù)端渲染SSR的示例代碼

    服務(wù)端渲染是一種常見(jiàn)的技術(shù)策略,特別是在需要改善網(wǎng)站的搜索引擎優(yōu)化(SEO)和首屏加載時(shí)間的場(chǎng)景下,本文將介紹如何利用?Python?實(shí)現(xiàn)?SSR,感興趣的可以了解下
    2024-02-02
  • 零基礎(chǔ)寫python爬蟲之打包生成exe文件

    零基礎(chǔ)寫python爬蟲之打包生成exe文件

    本文介紹了通過(guò)pyinstaller和pywin32兩個(gè)插件在windows環(huán)境下,將py文件打包成exe文件,有需要的朋友可以參考下
    2014-11-11
  • Python實(shí)現(xiàn)的拉格朗日插值法示例

    Python實(shí)現(xiàn)的拉格朗日插值法示例

    這篇文章主要介紹了Python實(shí)現(xiàn)的拉格朗日插值法,簡(jiǎn)單介紹了拉格朗日插值法的原理并結(jié)合完整實(shí)例形式給出了拉格朗日插值法的具體實(shí)現(xiàn)與使用技巧,需要的朋友可以參考下
    2019-01-01
  • python 3調(diào)用百度OCR API實(shí)現(xiàn)剪貼板文字識(shí)別

    python 3調(diào)用百度OCR API實(shí)現(xiàn)剪貼板文字識(shí)別

    這篇文章主要為大家詳細(xì)介紹了python 3調(diào)用百度OCR API實(shí)現(xiàn)剪貼板文字識(shí)別,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-09-09
  • Python實(shí)現(xiàn)為PDF去除水印的示例代碼

    Python實(shí)現(xiàn)為PDF去除水印的示例代碼

    這篇文章主要介紹了如何利用Python實(shí)現(xiàn)PDF去除水印功能,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-04-04
  • 使用python實(shí)現(xiàn)簡(jiǎn)單去水印功能

    使用python實(shí)現(xiàn)簡(jiǎn)單去水印功能

    這篇文章主要為大家詳細(xì)介紹了使用python實(shí)現(xiàn)簡(jiǎn)單去水印功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-05-05

最新評(píng)論