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