最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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父目錄、子目錄的相互調(diào)用方法

    Python父目錄、子目錄的相互調(diào)用方法

    今天小編就為大家分享一篇Python父目錄、子目錄的相互調(diào)用方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-02-02
  • 一篇文章徹底搞懂python正則表達(dá)式

    一篇文章徹底搞懂python正則表達(dá)式

    正則表達(dá)式是一個(gè)特殊的字符序列,它能幫助你方便的檢查一個(gè)字符串是否與某種模式匹配,Python 自1.5版本起增加了re模塊,這篇文章主要給大家介紹了如何通過(guò)一篇文章徹底搞懂python正則表達(dá)式的相關(guān)資料,需要的朋友可以參考下
    2021-09-09
  • python腳本編輯oss文件的實(shí)現(xiàn)示例

    python腳本編輯oss文件的實(shí)現(xiàn)示例

    本文主要介紹了python腳本編輯oss文件,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2024-05-05
  • Python Pandas 轉(zhuǎn)換unix時(shí)間戳方式

    Python Pandas 轉(zhuǎn)換unix時(shí)間戳方式

    今天小編就為大家分享一篇Python Pandas 轉(zhuǎn)換unix時(shí)間戳方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-12-12
  • pytorch cnn 識(shí)別手寫(xiě)的字實(shí)現(xiàn)自建圖片數(shù)據(jù)

    pytorch cnn 識(shí)別手寫(xiě)的字實(shí)現(xiàn)自建圖片數(shù)據(jù)

    這篇文章主要介紹了pytorch cnn 識(shí)別手寫(xiě)的字實(shí)現(xiàn)自建圖片數(shù)據(jù),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-05-05
  • python深度學(xué)習(xí)tensorflow卷積層示例教程

    python深度學(xué)習(xí)tensorflow卷積層示例教程

    這篇文章主要為大家介紹了python深度學(xué)習(xí)tensorflow卷積層示例教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-06-06
  • python數(shù)據(jù)處理實(shí)戰(zhàn)(必看篇)

    python數(shù)據(jù)處理實(shí)戰(zhàn)(必看篇)

    下面小編就為大家?guī)?lái)一篇python數(shù)據(jù)處理實(shí)戰(zhàn)(必看篇)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-06-06
  • 利用python中集合的唯一性實(shí)現(xiàn)去重

    利用python中集合的唯一性實(shí)現(xiàn)去重

    集合,用{ }表示,集合中所有元素具有唯一性。這篇文章給大家介紹利用python中集合的唯一性實(shí)現(xiàn)去重,感興趣的朋友一起看看吧
    2020-02-02
  • Python實(shí)現(xiàn)識(shí)別圖片和掃描PDF中的文字

    Python實(shí)現(xiàn)識(shí)別圖片和掃描PDF中的文字

    在處理掃描的PDF和圖片時(shí),文字信息往往無(wú)法直接編輯、搜索或復(fù)制,這給信息提取和分析帶來(lái)了諸多不便,所以本文將介紹如何使用Python及相關(guān)OCR庫(kù)實(shí)現(xiàn)對(duì)圖片和掃描PDF中文字的識(shí)別,需要的可以了解下
    2025-02-02
  • 詳解Python 切片語(yǔ)法

    詳解Python 切片語(yǔ)法

    Python的切片是特別常用的功能,主要用于對(duì)列表的元素取值。這篇文章主要介紹了詳解Python 切片語(yǔ)法,需要的朋友可以參考下
    2019-06-06

最新評(píng)論

佛学| 垣曲县| 宜川县| 萨迦县| 金华市| 林甸县| 新闻| 辛集市| 册亨县| 眉山市| 宜城市| 白山市| 湖州市| 屏南县| 南郑县| 资阳市| 齐河县| 庐江县| 罗山县| 介休市| 昭觉县| 鲁甸县| 绥棱县| 仲巴县| 呼玛县| 龙州县| 胶南市| 普兰店市| 四会市| 灵武市| 青州市| 洞头县| 乡城县| 华宁县| 尼勒克县| 南乐县| 息烽县| 沅陵县| 马山县| 北票市| 清流县|