Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 2.7 梯度下降-在python上使用一个线性函数来实现剩余函数_Python 2.7 - Fatal编程技术网

Python 2.7 梯度下降-在python上使用一个线性函数来实现剩余函数

Python 2.7 梯度下降-在python上使用一个线性函数来实现剩余函数,python-2.7,Python 2.7,希望清理我的代码。目前正在学习机器学习课程。缩短我的代码时出现问题: class LRGD(): def __init__(self, theta, x, y): self.theta = theta self.x = x self.y = y self.l = len(y) self.lt = len(theta) def residual(self): sum = 0

希望清理我的代码。目前正在学习机器学习课程。缩短我的代码时出现问题:

class LRGD():

    def __init__(self, theta, x, y):
        self.theta = theta
        self.x = x
        self.y = y
        self.l = len(y)
        self.lt = len(theta)

    def residual(self):
        sum = 0
        for j in range(self.l):
            # h = 0
            h = sum(self.theta[k] * self.x[k][j] for k in range(self.lt))
            # for k in range(self.lt):
            #     h += self.theta[k] * self.x[k][j]
            sum += (h-self.y[j])**2
        return sum/self.l

“剩余”功能正常工作,但我正在进行缩短,希望接近一个内衬。。无法找出未注释掉的h不起作用的原因->引发的错误是“int”不可调用。

您使用局部变量重写内置的
sum
函数:

def residual(self):
    sum = 0  # this overshaddows the built-in sum function
    for j in range(self.l):
        h = sum( ... )  # "sum" here is the variable, not the function
        ...

只需使用不同的变量名,例如
total=0

就可以用局部变量覆盖内置的
sum
函数:

def residual(self):
    sum = 0  # this overshaddows the built-in sum function
    for j in range(self.l):
        h = sum( ... )  # "sum" here is the variable, not the function
        ...
只需使用不同的变量名,例如
total=0