Python中针对n个功能的梯度下降?Andrew Ng的一部分';当然

Python中针对n个功能的梯度下降?Andrew Ng的一部分';当然,python,pandas,numpy,machine-learning,Python,Pandas,Numpy,Machine Learning,我目前正试图在Python3.8中实现梯度下降,但我在弄清楚如何让我的代码包含n个特性方面遇到了困难。我没有编程那么长时间,所以我对numpy和pandas不太熟悉,所以我的代码非常循环。我不关心运行时或快速收敛,但我想知道如何让GD为n个变量工作 我已尝试初始化PartialDerCal()中的θ列表,以将其传递到GDA()函数中。我猜我需要在PartialDerCal()函数中的某个地方使用一个“if”语句,但我不太清楚。这是我的稳定版本,它使用1个特性变量 import pandas as

我目前正试图在Python3.8中实现梯度下降,但我在弄清楚如何让我的代码包含n个特性方面遇到了困难。我没有编程那么长时间,所以我对numpy和pandas不太熟悉,所以我的代码非常循环。我不关心运行时或快速收敛,但我想知道如何让GD为n个变量工作

我已尝试初始化PartialDerCal()中的θ列表,以将其传递到GDA()函数中。我猜我需要在PartialDerCal()函数中的某个地方使用一个“if”语句,但我不太清楚。这是我的稳定版本,它使用1个特性变量

import pandas as pd #use pandas for loading and manipulating data
import numpy as np #use numpy for working with arrays
import matplotlib.pyplot as plt #use matplotlib for ploting

#loading data
def LoadData():
    #reading inputs and outputs from a text file
    data = pd.read_csv("data1.txt", header=None)
    return data

#calculate partial derivative
def PartialDerCal(theta_0,theta_1, data):      
    sum_theta_0 = 0
    sum_theta_1 = 0
    
    for index, row in data.iterrows(): #row[0]=x, row[1]=y
        h_theta_x = theta_0 + theta_1 * row[0]
        sum_theta_0 = sum_theta_0 + (h_theta_x - row[1])
        sum_theta_1 = sum_theta_1 + ((h_theta_x - row[1]) * row[0])
    return sum_theta_0, sum_theta_1

def CostCal(theta_0,theta_1, data, m):
    cost = 0
    for index, row in data.iterrows():
        h_theta_x = theta_0 + theta_1 * row[0]
        cost += ((1/(2*m)) * ((h_theta_x - row[1]) * ((h_theta_x) - row[1])))
    return cost
    
def GDA(data):
    
    convergence = 20
    convergence_iter = 0
    
    m = 97 #total points
    
    theta_0 = 0
    theta_1 = 0
    alpha = 0.01 
    
    theta_0s = []
    theta_1s = []
    costs = []
    
    theta_0s.append(theta_0)
    theta_1s.append(theta_1)
    temp_theta_0, temp_theta_1 = PartialDerCal(theta_0, theta_1, data)
    costs.append(CostCal(theta_0, theta_1, data, m))   
    
    while (convergence_iter < convergence):
        
        temp_theta_0 = theta_0 - (alpha * (1/m) * temp_theta_0)
        temp_theta_1 = theta_1 - (alpha * (1/m) * temp_theta_1)
        theta_0 = temp_theta_0
        theta_1 = temp_theta_1
        
        #Plot(theta_0, theta_1, data)
        theta_0s.append(theta_0)
        theta_1s.append(theta_1)
        temp_theta_0, temp_theta_1 = PartialDerCal(theta_0, theta_1, data)
        costs.append(CostCal(theta_0, theta_1, data, m))
        convergence_iter += 1
   
data = LoadData()
GDA(data)

        
data2.txt:

0.223753616,0.201342282,0.249946951
0.170154841,0.201342282,0.206195297
0.255232261,0.201342282,0.230633721
0.150587034,0.134228188,0.145005483
0.319040327,0.268456376,0.33745026
0.211098349,0.268456376,0.187444588
0.163135954,0.201342282,0.196819942
0.151756849,0.201342282,0.124379078
0.14675855,0.201342282,0.13250501
0.158882083,0.201342282,0.151568231
0.206312745,0.268456376,0.150005047
0.212693551,0.201342282,0.216883201
0.200995406,0.201342282,0.206257174
0.476220861,0.33557047,0.437454041
0.134847711,0.201342282,0.162443642
0.244597584,0.268456376,0.281198133
0.140377744,0.134228188,0.187444588
0.131444615,0.201342282,0.124942224
0.277458737,0.268456376,0.312510567
0.322337077,0.268456376,0.374389157
0.187914752,0.201342282,0.158068477
0.200782712,0.134228188,0.159381027
0.170580228,0.201342282,0.151818241
0.208652374,0.268456376,0.162443642
0.413688957,0.201342282,0.358701063
0.116981453,0.201342282,0.156193406
0.155053599,0.201342282,0.290323478
0.268631955,0.201342282,0.293136084
0.233962906,0.201342282,0.296886226
0.280436447,0.201342282,0.187444588
0.19557172,0.134228188,0.218695769
0.106346776,0.067114094,0.106191515
0.216947422,0.268456376,0.196819942
0.333609835,0.201342282,0.362451205
0.192594011,0.268456376,0.178694257
0.152820316,0.201342282,0.156193406
0.131763655,0.201342282,0.143692933
0.226731326,0.268456376,0.215633154
0.448251659,0.268456376,0.343137975
0.229921729,0.268456376,0.179381783
0.176961035,0.134228188,0.230321209
0.238004084,0.201342282,0.206195297
0.272992173,0.268456376,0.196257421
0.127616131,0.201342282,0.186882066
0.090607453,0.134228188,0.112441752
0.196954228,0.268456376,0.187444588
0.127935171,0.201342282,0.14969316

您是否可以发布代码以生成或显示文本以表示
data1.txt
?data1是1个输入变量,1个输出变量,而data2有2个输入变量。您的代码对这两个数据集都适用。究竟是什么问题?尝试在每次迭代时打印成本(在您的情况下,您可以做
print(costs[-1])
,因为您列出了一个列表),您将能够看到成本降低。您可以发布代码生成或文本表示
data1.txt
?data1是1个输入变量,1个输出变量,而data2有2个输入变量。您的代码对这两个数据集都适用。究竟是什么问题?尝试在每次迭代时打印成本(在您的情况下,您可以做
print(costs[-1])
,因为您列出了一个列表),您将能够看到成本降低。
0.223753616,0.201342282,0.249946951
0.170154841,0.201342282,0.206195297
0.255232261,0.201342282,0.230633721
0.150587034,0.134228188,0.145005483
0.319040327,0.268456376,0.33745026
0.211098349,0.268456376,0.187444588
0.163135954,0.201342282,0.196819942
0.151756849,0.201342282,0.124379078
0.14675855,0.201342282,0.13250501
0.158882083,0.201342282,0.151568231
0.206312745,0.268456376,0.150005047
0.212693551,0.201342282,0.216883201
0.200995406,0.201342282,0.206257174
0.476220861,0.33557047,0.437454041
0.134847711,0.201342282,0.162443642
0.244597584,0.268456376,0.281198133
0.140377744,0.134228188,0.187444588
0.131444615,0.201342282,0.124942224
0.277458737,0.268456376,0.312510567
0.322337077,0.268456376,0.374389157
0.187914752,0.201342282,0.158068477
0.200782712,0.134228188,0.159381027
0.170580228,0.201342282,0.151818241
0.208652374,0.268456376,0.162443642
0.413688957,0.201342282,0.358701063
0.116981453,0.201342282,0.156193406
0.155053599,0.201342282,0.290323478
0.268631955,0.201342282,0.293136084
0.233962906,0.201342282,0.296886226
0.280436447,0.201342282,0.187444588
0.19557172,0.134228188,0.218695769
0.106346776,0.067114094,0.106191515
0.216947422,0.268456376,0.196819942
0.333609835,0.201342282,0.362451205
0.192594011,0.268456376,0.178694257
0.152820316,0.201342282,0.156193406
0.131763655,0.201342282,0.143692933
0.226731326,0.268456376,0.215633154
0.448251659,0.268456376,0.343137975
0.229921729,0.268456376,0.179381783
0.176961035,0.134228188,0.230321209
0.238004084,0.201342282,0.206195297
0.272992173,0.268456376,0.196257421
0.127616131,0.201342282,0.186882066
0.090607453,0.134228188,0.112441752
0.196954228,0.268456376,0.187444588
0.127935171,0.201342282,0.14969316