提问人:hghebrem 提问时间:1/12/2023 更新时间:1/12/2023 访问量:67
如何从父类继承返回值并将它们绘制在子类中?
how do i Inherit return values from parent class and plot them in the child class?
问:
所以我有一个工作正常的父类,但我正在努力从父类中获取返回值并用子类绘制它们。 理想情况下,我会从父类中调用一个方法(子类调用该方法)并使用子类中的返回值并绘制它们。子类应该能够采用与父类相同的参数。
父类:
from statsmodels.stats.power import FTestAnovaPower
class PwrAnalysis:
def __init__(self, effect_size=None, nobs=None, alpha=None, alpha2=None, power=None, k_groups=None, rng1=None, rng2=None):
self.effect_size = effect_size
self.nobs = nobs
self.alpha = alpha
self.alpha2 = alpha2
self.power = power
self.k_groups = k_groups
self.rng1 = rng1
self.rng2 = rng2
def pwr_vs_smpl_sz(self):
pwr_vs_smpl_1 = []
pwr_vs_smpl_2 = []
for pwr_rng in self.rng1:
pwr_vs_smpl_1.append(FTestAnovaPower().solve_power(effect_size=self.effect_size,
nobs=None, alpha=self.alpha, power=pwr_rng, k_groups=self.k_groups))
for pwr_rng in self.rng2:
pwr_vs_smpl_2.append(FTestAnovaPower().solve_power(effect_size=self.effect_size,
nobs=None, alpha=self.alpha2, power=pwr_rng, k_groups=self.k_groups))
return [pwr_vs_smpl_1, self.rng1], [pwr_vs_smpl_2, self.rng2]
def pwr_vs_effct_sz(self):
pwr_vs_effect_size_1 = []
pwr_vs_effect_size_2 = []
for pwr_rng in self.rng1:
pwr_vs_effect_size_2.append(FTestAnovaPower().solve_power(effect_size=None,
nobs=self.nobs, alpha=self.alpha, power=pwr_rng, k_groups=self.k_groups))
for pwr_rng in self.rng2:
pwr_vs_effect_size_2.append(FTestAnovaPower().solve_power(effect_size=None,
nobs=self.nobs, alpha=self.alpha2, power=pwr_rng, k_groups=self.k_groups))
return [pwr_vs_effect_size_1, self.rng1], [pwr_vs_effect_size_2, self.rng2]
def smpl_sz_vs_effct_sz(self):
sample_size_vs_effect_size_1 = []
sample_size_vs_effect_size_2 = []
for num_of_smpls in self.rng1:
sample_size_vs_effect_size_1.append(FTestAnovaPower().solve_power(effect_size=None,
nobs=num_of_smpls, alpha=self.alpha, power=self.power, k_groups=self.k_groups))
for num_of_smpls in self.rng2:
sample_size_vs_effect_size_2.append(FTestAnovaPower().solve_power(effect_size=None,
nobs=num_of_smpls, alpha=self.alpha2, power=self.power, k_groups=self.k_groups))
return [sample_size_vs_effect_size_1, self.rng1], [sample_size_vs_effect_size_2, self.rng2]
儿童班:
class PwrPlots(PwrAnalysis):
def __init__(self, effect_size=None, nobs=None, alpha=None, alpha2=None, power=None, k_groups=None, rng1=None, rng2=None):
super().__init__(effect_size=None, nobs=None, alpha=None, alpha2=None, power=None, k_groups=None, rng1=None, rng2=None)
def plt_pwr_vs_smpl(self):
# plot 1
plt.plot(super().pwr_vs_smpl_1, self.rng1, 'b', marker='o', label=f'alpha={self.alpha:.2f}')
#plot 2
plt.plot(super().pwr_vs_smpl_2, self.rng2, 'b', marker='o', label=f'alpha={self.alpha:.2f}')
# # labels, title, legend etc...
# plt.title('Power vs Sample size')
# plt.xlabel('Power')
# plt.ylabel('Sample Size')
# plt.legend(loc="lower right")
# #plt.grid()
# def plt_pwr_vs_effct_sz(self):
# def plt_smpl_sz_vs_effct_sz(self):
答:
1赞
kosciej16
1/12/2023
#1
您应该调用该父函数,并只取它返回的值:
class PwrPlots(PwrAnalysis):
def plt_pwr_vs_smpl(self):
# you can use self if you don't override that function in base class
result = self.pwr_vs_smpl_sz()
# result contains list of two elements
pwr_vs_smpl_1, pwr_vs_smpl_2 = result[0][0], result[1][0]
# plot 1
plt.plot(pwr_vs_smpl_1, self.rng1, 'b', marker='o', label=f'alpha={self.alpha:.2f}')
...
下一个:如何继承和初始化父类方法
评论
super()
self.pwr_vs_smpl_1(...)
pwr_vs_smpl_1
不是父类中的方法。它是方法内部的局部变量,在其他任何地方都无法访问。pwr_vs_smpl_sz()
PwrPlots.plt_pwr_vs_smpl
PwrAnalysis