如何将类中函数返回的变量传递给该类中的另一个函数?
例如(实现线性回归):
class ForwardPropagation(object):
def __init__(self, xs, realoutput):
self.xs = np.asarray(xs)
self.realoutput = np.asarray(realoutput)
self.guess_slope = random.randint(1,10)
self.guess_b = random.randint(1,10)
self.guess_output = (self.guess_slope * self.xs) + self.guess_b
def distance(self):#returns np.array with distance results
abs_D = self.realoutput - self.guess_output
D_sqr = abs_D ** 2
return abs_D, D_sqr
def error(self):
sigma = np.sum(D_sqr)
mean_error = sigma / len(D_sqr)
return mean_error
如何将变量D_sqr(我的距离函数的平方)传递给误差函数?
答案 0 :(得分:3)
当你可以调用其他函数时,你不需要传递任何东西
def distance(self):#returns np.array with distance results
abs_D = self.realoutput - self.guess_output
D_sqr = abs_D ** 2
return abs_D, D_sqr
def error(self):
abs_D, D_sqr = self.distance()
sigma = np.sum(D_sqr)
mean_error = sigma / len(D_sqr)
return mean_error
或者你可以重新计算它
def error(self):
abs_D = self.realoutput - self.guess_output
D_sqr = abs_D ** 2
sigma = np.sum(D_sqr)
mean_error = sigma / len(D_sqr)
return mean_error
答案 1 :(得分:0)
使用类变量
self.D_sqr = D_sqr
错误地说:
sigma = np.sum(self.D_sqr)