本文介绍了scipy.stats.rv_discrete 子类的实例在 pmf() 方法上引发错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想创建一个 scipy.stats.rv_discrete
的子类来添加一些额外的方法.但是,当我尝试访问子类的 pmf()
方法时,会引发错误.请看下面的例子:
I want to create a subsclass of scipy.stats.rv_discrete
to add some additional methods. However, when I try to access the pmf()
method of the subclass, an error is raised. Please see the following example:
import numpy as np
from scipy import stats
class sub_rv_discrete(stats.rv_discrete):
pass
xk = np.arange(2)
pk = (0.5, 0.5)
instance_subclass = sub_rv_discrete(values=(xk, pk))
instance_subclass.pmf(xk)
结果:
Traceback (most recent call last):
File "<ipython-input-48-129655c38e6a>", line 11, in <module>
instance.pmf(xk)
File "C:\Anaconda3\lib\site-packages\scipy\stats\_distn_infrastructure.py", line 2832, in pmf
args, loc, _ = self._parse_args(*args, **kwds)
AttributeError: 'rv_sample' object has no attribute '_parse_args'
尽管如此,如果我直接使用 stats.rv_discrete
,一切都很好:
Despite that, if I use stats.rv_discrete
directly, everything is fine:
instance_class = stats.rv_discrete(values=(xk, pk))
instance_class.pmf(xk)
---> array([ 0.5, 0.5])
推荐答案
@josef-pkt 对 github 如下:
通过常规的子类化创建一个 rv_sample 类并且不会初始化正确的类
以下适用于 0.18.1(我现在已经打开)
the following works for me with 0.18.1 (which I have open right now)
from scipy.stats._distn_infrastructure import rv_sample
class subc_rv_discrete(rv_sample):
def __new__(cls, *args, **kwds):
return super(subc_rv_discrete, cls).__new__(cls)
xk = [0,1,2,3]
pk = [0.1, 0.2, 0.3, 0.4]
inst = subc_rv_discrete(values=(xk, pk))
print(inst.pmf(xk))
print(inst.__class__)
也许这将在进一步的 scipy
版本中修复...
maybe this will be fixed in further scipy
releases...
这篇关于scipy.stats.rv_discrete 子类的实例在 pmf() 方法上引发错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!