Accessing a variable of one method inside another method in the same class – Python

huangapple go评论100阅读模式
英文:

Accessing a variable of one method inside another method in the same class - Python

问题

以下是代码部分的中文翻译:

  1. class MandM:
  2. def __init__(self, data, X=2, y=2):
  3. self.data = data
  4. self.X = X
  5. self.y = y
  6. def _ols(self):
  7. y = self.data.iloc[:, :y]
  8. X = self.data.iloc[:, X:]
  9. B = pd.DataFrame(inv(np.dot(X.T, X)) @ np.dot(X.T, y))
  10. B.columns = list(y.columns)
  11. B.index = list(X.columns)
  12. yhat = X @ B
  13. return {"B": B, "yhat": yhat, "y": y, "X": X}
  14. def _sscp(self):
  15. # 调用ols方法
  16. ols = MandM(self.data)._ols()
  17. ybar = pd.DataFrame([ols["y"].mean()] * ols["y"].shape[0])
  18. y_ybar = ols["y"] - ybar
  19. sscp_tot = pd.DataFrame(np.dot(y_ybar.T, y_ybar))
  20. sscp_tot.columns = list(ols["y"].columns)
  21. sscp_tot.index = list(ols["y"].columns)
  22. yhat_ybar = ols["yhat"] - ybar
  23. sscp_reg = pd.DataFrame(np.dot(yhat_ybar.T, yhat_ybar))
  24. sscp_reg.columns = list(ols["y"].columns)
  25. sscp_reg.index = list(ols["y"].columns)
  26. resid = ols["y"] - ols["X"] @ ols["B"]
  27. y_yhat = ols["y"] - ols["yhat"]
  28. sscp_resid = pd.DataFrame(np.dot(y_yhat.T, y_yhat))
  29. sscp_resid.columns = list(ols["y"].columns)
  30. sscp_resid.index = list(ols["y"].columns)
  31. return {"sscp_tot": sscp_tot, "sscp_reg": sscp_reg,
  32. "resid": resid, "sscp_resid": sscp_resid}
英文:

Let's say I have class, MandM, as indicated below with different methods. I want all the variables in each method to be accessible to all other methods in the class because I may need them in other methods.

So what I am doing is like in the example below, where I call a method within another method and then access the variables I want from that method. For each method, I return its output as a dictionary and then I can refer to that variable by name in another method.

My question is, is there another more efficient way to go about this?

  1. class MandM:
  2. def __init__(self, data, X=2, y=2):
  3. self.data = data
  4. self.X = X
  5. self.y = y
  6. def _ols(self):
  7. y = self.data.iloc[:, :y]
  8. X = self.data.iloc[:, X:]
  9. B = pd.DataFrame(inv(np.dot(X.T, X)) @ np.dot(X.T, y))
  10. B.columns = list(y.columns)
  11. B.index = list(X.columns)
  12. yhat = X @ B
  13. return {"B": B, "yhat": yhat, "y": y, "X": X}
  14. def _sscp(self):
  15. # call ols method
  16. ols = MandM(self.data)._ols()
  17. ybar = pd.DataFrame([ols["y"].mean()] * ols["y"].shape[0])
  18. y_ybar = ols["y"] - ybar
  19. sscp_tot = pd.DataFrame(np.dot(y_ybar.T, y_ybar))
  20. sscp_tot.columns = list(ols["y"].columns)
  21. sscp_tot.index = list(ols["y"].columns)
  22. yhat_ybar = ols["yhat"] - ybar
  23. sscp_reg = pd.DataFrame(np.dot(yhat_ybar.T, yhat_ybar))
  24. sscp_reg.columns = list(ols["y"].columns)
  25. sscp_reg.index = list(ols["y"].columns)
  26. resid = ols["y"] - ols["X"] @ ols["B"]
  27. y_yhat = ols["y"] - ols["yhat"]
  28. sscp_resid = pd.DataFrame(np.dot(y_yhat.T, y_yhat))
  29. sscp_resid.columns = list(ols["y"].columns)
  30. sscp_resid.index = list(ols["y"].columns)
  31. return {"sscp_tot": sscp_tot, "sscp_reg": sscp_reg,
  32. "resid": resid, "sscp_resid": sscp_resid}

答案1

得分: 3

你可以在__init__方法中创建所有内容,并将它们设置为实例变量,例如self.Bself.ywhat等等。

但如果你更喜欢按需加载它们(惰性加载),可以使用类似这样的属性:

  1. class MandM:
  2. def __init__(self, data, x=2, y=2):
  3. self.data = data
  4. self.x = x
  5. self.y = y
  6. self._xvalues = None
  7. self._yvalues = None
  8. self._b = None
  9. self._ywhat = None
  10. @property
  11. def xvalues(self):
  12. if self._xvalues is None:
  13. self._xvalues = self.data.iloc[:, :self.x]
  14. return self._xvalues
  15. @property
  16. def yvalues(self):
  17. if self._yvalues is None:
  18. self._yvalues = self.data.iloc[:, :self.y]
  19. return self._yvalues
  20. @property
  21. def ywhat(self):
  22. if self._ywhat is None:
  23. self._ywhat = X @ self.b
  24. return self._ywhat
  25. @property
  26. def b(self):
  27. if self._b is None:
  28. B = pd.DataFrame(inv(np.dot(self.xvalues.T, self.xvalues)) @ np.dot(self.xvalues.T, self.yvalues))
  29. B.columns = list(self.yvalues.columns)
  30. B.index = list(self.xvalues.columns)
  31. self._b = B
  32. return self._b

你还可以编写计算多个值并将它们设置为实例变量的方法,这样你可以在其他方法中重复使用它们:

  1. def _sscp(self):
  2. ybar = pd.DataFrame([self.yvalues.mean()] * self.yvalues.shape[0])
  3. y_ybar = self.yvalues - ybar
  4. sscp_tot = pd.DataFrame(np.dot(y_ybar.T, y_ybar))
  5. sscp_tot.columns = list(self.yvalues.columns)
  6. sscp_tot.index = list(self.yvalues.columns)
  7. yhat_ybar = self.ywhat - ybar
  8. sscp_reg = pd.DataFrame(np.dot(yhat_ybar.T, yhat_ybar))
  9. sscp_reg.columns = list(self.yvalues.columns)
  10. sscp_reg.index = list(self.yvalues.columns)
  11. resid = self.yvalues - self.xvalues @ self.b
  12. y_yhat = self.yvalues - self.ywhat
  13. sscp_resid = pd.DataFrame(np.dot(y_yhat.T, y_yhat))
  14. sscp_resid.columns = list(self.yvalues.columns)
  15. sscp_resid.index = list(self.yvalues.columns)
  16. self.sscp_tot = sscp_tot
  17. self.sscp_reg = sscp_reg
  18. self.resid = resid
  19. self.sscp_resid = sscp_resid

希望这有帮助!

英文:

You could just created everything in the __init__ method and set them as instance variable self.B, self.ywhat, ...

But if you prefer loading them on-demande (lazily), use properties like that :

  1. class MandM:
  2. def __init__(self, data, x=2, y=2):
  3. self.data = data
  4. self.x = x
  5. self.y = y
  6. self._xvalues = None
  7. self._yvalues = None
  8. self._b = None
  9. self._ywhat = None
  10. @property
  11. def xvalues(self):
  12. if self._xvalues is None:
  13. self._xvalues = self.data.iloc[:, :self.x]
  14. return self._xvalues
  15. @property
  16. def yvalues(self):
  17. if self._yvalues is None:
  18. self._yvalues = self.data.iloc[:, :self.y]
  19. return self._yvalues
  20. @property
  21. def ywhat(self):
  22. if self._ywhat is None:
  23. self._ywhat = X @ self.b
  24. return self._ywhat
  25. @property
  26. def b(self):
  27. if self._b is None:
  28. B = pd.DataFrame(inv(np.dot(self.xvalues.T, self.xvalues)) @ np.dot(self.xvalues.T, self.yvalues))
  29. B.columns = list(self.yvalues.columns)
  30. B.index = list(self.xvalues.columns)
  31. self._b = B
  32. return self._b

You can also have methods that compute multiple values and set them as instance variable, so you can reuse them in other methods

  1. def _sscp(self):
  2. ybar = pd.DataFrame([self.yvalues.mean()] * self.yvalues.shape[0])
  3. y_ybar = self.yvalues - ybar
  4. sscp_tot = pd.DataFrame(np.dot(y_ybar.T, y_ybar))
  5. sscp_tot.columns = list(self.yvalues.columns)
  6. sscp_tot.index = list(self.yvalues.columns)
  7. yhat_ybar = self.ywhat - ybar
  8. sscp_reg = pd.DataFrame(np.dot(yhat_ybar.T, yhat_ybar))
  9. sscp_reg.columns = list(self.yvalues.columns)
  10. sscp_reg.index = list(self.yvalues.columns)
  11. resid = self.yvalues - self.xvalues @ self.b
  12. y_yhat = self.yvalues - self.ywhat
  13. sscp_resid = pd.DataFrame(np.dot(y_yhat.T, y_yhat))
  14. sscp_resid.columns = list(self.yvalues.columns)
  15. sscp_resid.index = list(self.yvalues.columns)
  16. self.sscp_tot = sscp_tot
  17. self.sscp_reg = sscp_reg
  18. self.resid = resid
  19. self.sscp_resid = sscp_resid

huangapple
  • 本文由 发表于 2023年5月21日 20:46:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76299972.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定