本文介绍了如何子类scipy.stats.norm?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想继承 scipy.stats.norm ,以便我可以拥有具有附加功能的冻结分布实例(即具有特定方法/差异)。
但是,我无法完成构建实例的第一步。

I'd like to subclass scipy.stats.norm so that I can have instances of frozen distributions (i.e. with specific means/variances) with additional functionality.However, I can't get past the first step of constructing an instance.

编辑:这是一个成绩单交互式会话,演示我的问题(我的袖子里没有任何东西)

Edit: here is a transcript of an interactive session that demonstrates my problem (there's nothing up my sleeves)

In [1]: import scipy.stats

In [2]: class A(scipy.stats.norm):
   ...:     def __init__(self):
   ...:         super( A, self).__init__()
   ...:
   ...:
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)

/home/Dave/src/python2.7/density_estimation/<ipython console> in <module>()

/usr/lib64/python2.7/site-packages/scipy/stats/distributions.pyc in __init__(self, momtype, a, b, xa, xb, xtol, badvalue, name, longname, shapes, extradoc)
    958
    959         if longname is None:
--> 960             if name[0] in ['aeiouAEIOU']:
    961                 hstr = "An "
    962             else:

TypeError: Error when calling the metaclass bases
    'NoneType' object is not subscriptable

我可以看到 scipy.stats 正在做某种奇怪的事情,其中​​ norm 是某个特定的实例(sometype?),但它不是正常的类定义,所以我不喜欢看看如何为它调用构造函数。

I can see that scipy.stats is doing some sort of weird thing where norm is a specific instance of something (sometype?), but it's not a normal class definition, so I don't see how to invoke a constructor for it.

编辑#2:scipy版本可能是相关的。

Edit #2: scipy version may be relevant.

In [19]: scipy.__version__
Out[19]: '0.9.0'


推荐答案

scipy.stats.norm 不是一个类。它是 scipy.stats.norm_gen 的一个实例。调用 norm(* args,** kwds)将返回 rv_frozen 的实例,其中包含 norm 以及你给它的参数。如果你想要一种新的冻结分布,子类 rv_frozen 来添加你的方法,只需用 norm 实例化它,参数。不要担心继承 norm_gen

scipy.stats.norm is not a class. It is an instance of scipy.stats.norm_gen. Calling norm(*args, **kwds) will return an instance of rv_frozen with norm and the arguments that you gave it. If you want a new kind of frozen distribution, subclass rv_frozen to add your methods and just instantiate it with norm and the arguments. Don't worry about subclassing norm_gen.

这篇关于如何子类scipy.stats.norm?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 12:35