梯度下降算法

问题描述 投票:0回答:0

我最近实现了线性回归的梯度下降代码。但是,当我增加迭代次数时,“w”和“c”的值会与迭代次数成比例地增加。谁能告诉我问题出在哪里。 您可以使用数据集来定义“x”和“y”

w = c = 0
alpha = 0.0001
y_calc = w * x + c
n = len(x)
p = float(n)
u = 0
for u in range(100000):
    w = w + alpha * ((1/p) * np.sum(l * (y - y_calc)))
    c = c + alpha * ((1/p) * np.sum(y - y_calc))
    u += 1
print(w,c)

x = [32.50234527, 53.42680403, 61.53035803, 47.47563963, 59.81320787, 55.14218841、52.21179669、39.29956669、48.10504169、52.55001444、 45.41973014、54.35163488、44.1640495、58.16847072、56.72720806、 48.95588857, 44.68719623, 60.29732685, 45.61864377, 38.81681754]

y = [31.70700585, 68.77759598, 62.5623823, 71.54663223, 87.23092513, 78.21151827、79.64197305、59.17148932、75.3312423、71.30087989、 55.16567715、82.47884676、62.00892325、75.39287043、81.43619216、 60.72360244、82.89250373、97.37989686、48.84715332、56.87721319]

预计 w 为 1.389738813163012,c 为 0.03509461674147458

machine-learning linear-regression computer-science gradient-descent supervised-learning
© www.soinside.com 2019 - 2024. All rights reserved.