当前位置: 首页 > 知识库问答 >
问题:

梯度下降、残差平方和溢出的线性回归

党俊健
2023-03-14

你好,我试图学习回归算法,并与此同时,我试图实现梯度下降的线性回归,并使用平方的残差和来确定收敛性。我注意到在迭代的某个时候,平方的残差和的求值,我认为这是有意义的,但我不知道如何解决这个问题。我做错了什么吗?

import math
import numpy as num

def get_regression_predictions(input_feature, intercept, slope):
   predicted_output = [intercept + xi*slope for xi in input_feature]
   return(predicted_output)

def get_residual_sum_of_squares(input_feature, output, intercept,slope):
   return num.sum( [(output.iloc[i] - (intercept + slope*input_feature.iloc[i]))**2 for i in range(0,len(output))] )

def train(input_feature,output,intercept,slope):
#the start value of intercept and slope are 0
    last = 0
    now = math.sqrt(get_residual_sum_of_squares(input_feature,output,intercept,slope))

    while abs(last - now) >= 0.01:
        last = now
        predictions = get_regression_predictions(input_feature,intercept,slope)
        errors = [output.iloc[i] - predictions[i] for i in range(0,len(predictions))]


        adjustements = (sum(errors)*0.05,sum([errors[i]*output.iloc[i] for i in range(0,len(errors))] ) *0.05)

        intercept ,slope = (intercept - adjustements[0],slope - adjustements[1] )
        now = math.sqrt(get_residual_sum_of_squares(input_feature,output,intercept,slope))

 return intercept,slope 

共有1个答案

楚弘益
2023-03-14

尝试随机梯度下降。您可能总结了许多导致溢出的实例错误。

 类似资料:
  • 我试图在java中实现线性回归。我的假设是θ0θ1*x[i]。我试图计算θ0和θ1的值,使成本函数最小。我正在用梯度下降来找出值- 在 在收敛之前,这种重复是什么?我知道这是局部最小值,但我应该在while循环中输入的确切代码是什么? 我对机器学习非常陌生,刚开始编写基本的算法以获得更好的理解。任何帮助都将不胜感激。

  • 我试图在MatLab中实现一个函数,该函数使用牛顿法计算最佳线性回归。然而,我陷入了一个问题。我不知道如何求二阶导数。所以我不能实施它。这是我的密码。 谢谢你的帮助。 编辑:: 我用一些纸和笔解决了这个问题。你所需要的只是一些微积分和矩阵运算。我找到了二阶导数,它现在正在工作。我正在为感兴趣的人分享我的工作代码。

  • 在机器学习课程https://share.coursera.org/wiki/index.php/ML:Linear_Regression_with_Multiple_Variables#Gradient_Descent_for_Multiple_Variables中,它说梯度下降应该收敛。 我正在使用scikit学习的线性回归。它不提供梯度下降信息。我已经看到了许多关于stackoverflow

  • 我用JavaScript实现了一个非常简单的线性回归和梯度下降算法,但是在查阅了多个源代码并尝试了几件事情之后,我无法使它收敛。 数据是绝对线性的,只是数字0到30作为输入,x*3作为正确的输出来学习。 这就是梯度下降背后的逻辑: 我从不同的地方取了公式,包括: 乌达城深度学习基金会纳米学位的练习 吴恩达的线性回归梯度下降课程(也在这里) 斯坦福CS229讲义 我从卡内基梅隆大学找到的其他PDF幻

  • 我试图实现梯度下降的线性回归,如本文(https://towardsdatascience.com/linear-regression-using-gradient-descent-97a6c8700931)所述。我已经严格遵循了实现,但是经过几次迭代后,我的结果会溢出。我试图得到这个结果大约: y=-0.02x 8499.6。 代码: 在这里,它可以在围棋场上工作:https://play.go

  • 我正在学习机器学习/线性回归的Coursera课程。下面是他们如何描述用于求解估计OLS系数的梯度下降算法: 因此,他们对系数使用,对设计矩阵(或他们称之为特征)使用,对因变量使用。它们的收敛准则通常是RSS梯度的范数小于容差ε;也就是说,他们对“不收敛”的定义是: 我很难让这个算法收敛,我想知道在我的实现中是否忽略了一些东西。下面是代码。请注意,我还通过statsmodels回归库运行了我在其中