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

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

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

python使用梯度下降算法實現(xiàn)一個多線性回歸,供大家參考,具體內(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
 # 通過每次觀察進行循環(huán)
 for i in range(m):
 # Compute the hypothesis
 # 計算假設(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

# 對theta0 進行求導(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 當前第幾次
 c = cost(theta0, theta1, theta2, theta3, theta4, x1, x2, x3, x4, y) ## Initial cost 當前代價函數(shù)
 costs = [c] # Lets store each update 每次損失值都記錄下來
 # 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è)置一個收斂的閾值 (兩次迭代目標函數(shù)值相差沒有相差多少,就可以停止了)
 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
 # 兩次間隔迭代目標函數(shù)值相差沒有相差多少(說明可以停止了)
 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ù)相當于步長
 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
 # 當前迭代之后,參數(shù)發(fā)生更新
 c = cost(theta0, theta1, theta2, theta3, theta4, x1, x2, x3, x4, y)

 # Store updates,可以進行保存當前代價值
 costs.append(c)
 counter += 1 # Count
 # 將當前的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ù)變換圖:

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

相關(guān)文章

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

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

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

    一篇文章徹底搞懂python正則表達式

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    詳解Python 切片語法

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

最新評論

峨山| 云浮市| 玛纳斯县| 甘孜县| 文登市| 勃利县| 达日县| 密山市| 克什克腾旗| 安平县| 远安县| 施秉县| 海原县| 乌拉特后旗| 和林格尔县| 华坪县| 化州市| 桃园县| 喀喇沁旗| 万载县| 益阳市| 大邑县| 南郑县| 包头市| 平湖市| 吉首市| 恩施市| 南阳市| 舞钢市| 林口县| 晋江市| 安新县| 中西区| 剑川县| 巨野县| 慈溪市| 车致| 鹤山市| 阳东县| 阿荣旗| 临武县|