Python Scipy.Stats.exponweib的子类不调用_init__或其他函数
我试图创建一个子类来简化对Python Scipy.Stats.exponweib的子类不调用_init__或其他函数,python,class,scipy,Python,Class,Scipy,我试图创建一个子类来简化对scipy.stats.exponweib包的输入,并添加一些额外的函数。如果类在自己的文件中调用weibull.py from scipy.stats import exponweib # import scipy class weibull(exponweib): def __init__(self,beta,nu): super(weibull,self).__init__(a=1,c=beta,loc=0,scale=nu)
scipy.stats.exponweib
包的输入,并添加一些额外的函数。如果类在自己的文件中调用weibull.py
from scipy.stats import exponweib
# import scipy
class weibull(exponweib):
def __init__(self,beta,nu):
super(weibull,self).__init__(a=1,c=beta,loc=0,scale=nu)
print beta
print nu
def doSomething(self,s):
print(s)
我的测试脚本如下所示:
from weibull import weibull
w = weibull(2.6,2600)
print('%0.10f'%w.pdf(1000))
w.doSomething('Something')
似乎我的\uuuu init\uuuu
根本没有运行,没有任何打印语句运行,并且doSomething
例程上抛出了一个错误
终端中的输出如下所示:
Codes> python weibull_testHarness.py
0.0000000000
Traceback (most recent call last):
File "weibull_testHarness.py", line 5, in <module>
w.doSomething('Something')
AttributeError: 'rv_frozen' object has no attribute 'doSomething'
Codes>python weibull\u testHarness.py
0
回溯(最近一次呼叫最后一次):
文件“weibull_testHarness.py”,第5行,在
w、 doSomething(“某物”)
AttributeError:“rv_冻结”对象没有属性“doSomething”
每个NumPy/SciPy开发者,子类rv_-freezed
,而不是exponweib
请注意,exponweib
是一个
因此,按照这种模式,通过类比,w=weibull(2.62600)
将是rv\u-freezed
的一个实例。如果希望w
具有其他方法,则需要将rv\u freezed
子类化,而不是exponweib
,也不是exponweib\u gen
,也不是rv\u continuous
屈服
{'loc': 0, 'scale': 2600, 'c': 2.6, 'a': 1}
0.0001994484
Something
我不认为exponweib本身就是一个类。运行
导入检查;打印(inspect.isclass(exponweib))
以自行验证。该类为rv_continuous
或exponweib_gen
,并且rv_continuous
作为从scipy.stats导入rv_continuous
导入。我认为这就是你需要继承的东西,才能进行修改。尝试并报告它是否工作。exponweib
您导入的是一个对象exponweib\u gen
可能是您正在寻找的类。只是好奇,为什么原始的类weibull(exponweib):
是合法的?我认为可调用不是创建类的唯一要求。@HaochenWu:class-Foo(Bar)
。如果(错误地)尝试使用类weibull(exponweib)
定义类,Python将调用类型(exponweib)('weibull',(exponweib,),clsdict)
,这将返回exponweib\u gen
的实例。它是“合法的”Python——它将'weibull'
分配给momtype
和'(exponweib,)
分配给a
和clsdict
分配给b
。在此阶段不会发生错误,但它不会返回合理的类(或合理的分布)。
In [107]: exponweib(a=1, c=2.6)
Out[107]: <scipy.stats._distn_infrastructure.rv_frozen at 0x7fd7997282b0>
import scipy.stats as stats
class my_frozen(stats._distn_infrastructure.rv_frozen):
def __init__(self, dist, *args, **kwds):
super(my_frozen,self).__init__(dist, *args, **kwds)
print(kwds)
def doSomething(self,s):
print(s)
def weibull(beta, nu):
dist = stats.exponweib # an instance of stats._continuous_distns.exponweib_gen
dist.name = 'weibull'
return my_frozen(dist, a=1, c=beta, loc=0, scale=nu)
w = weibull(2.6, 2600)
print('%0.10f'%w.pdf(1000))
w.doSomething('Something')
{'loc': 0, 'scale': 2600, 'c': 2.6, 'a': 1}
0.0001994484
Something