2016-06-06 2 views
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 

'잔류'기능을하지만 잘하면 가까운 하나 라이너, 단축 일하고 제대로 작동을 .. 알아낼 수없는 '시간 ISN을 주석 처리하지 왜 t 작업 -> throw 된 오류는 'int'를 호출 할 수 없습니다.

답변

2

당신은 우선 내장 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.

+1

감사! 어리석은 실수 – pauld

관련 문제