如何从冻结的Spicer.stats分布中获取参数自变量? [英] How to get parameter arguments from a frozen spicy.stats distribution?
问题描述
在 scipy.stats
中您可以创建 冻结分布,从而可以为该实例永久设置分布的参数化(形状,位置和比例).
In scipy.stats
you can create a frozen distribution that allows the parameterization (shape, location & scale) of the distribution to be permanently set for that instance.
For example, you can create an gamma distribution (scipy.stats.gamma
) with a
,loc
and scale
parameters and freeze them so they do not have to be passed around every time that distribution is needed.
import scipy.stats as stats
# Parameters for this particular gamma distribution
a, loc, scale = 3.14, 5.0, 2.0
# Do something with the general distribution parameterized
print 'gamma stats:', stats.gamma(a, loc=loc, scale=scale).stats()
# Create frozen distribution
rv = stats.gamma(a, loc=loc, scale=scale)
# Do something with the specific, already parameterized, distribution
print 'rv stats :', rv.stats()
gamma stats: (array(11.280000000000001), array(12.56))
rv stats : (array(11.280000000000001), array(12.56))
可访问的rv
参数?
由于此功能很可能不会传递参数,因此是否有办法在以后仅从冻结的分布rv
中取回这些值?
Accessible rv
parameters?
Since the parameters will most likely not be passed around as a result of this feature, is there a way to get those values back from only the frozen distribution, rv
, later on?
推荐答案
访问rv
冻结参数
是的,在分发实例中可以使用用于创建冻结分发的参数.它们存储在 args
& kwds
属性.这取决于发行版的实例是使用位置参数还是关键字参数创建的.
Accessing rv
frozen parameters
Yes, the parameters used to create a frozen distribution are available within the instance of the distribution. They are stored within the args
& kwds
attribute. This will be dependent on if the distribution's instance was created with positional arguments or keyword arguments.
import scipy.stats as stats
# Parameters for this particular alpha distribution
a, loc, scale = 3.14, 5.0, 2.0
# Create frozen distribution
rv1 = stats.gamma(a, loc, scale)
rv2 = stats.gamma(a, loc=loc, scale=scale)
# Do something with frozen parameters
print 'positional and keyword'
print 'frozen args : {}'.format(rv1.args)
print 'frozen kwds : {}'.format(rv1.kwds)
print
print 'positional only'
print 'frozen args : {}'.format(rv2.args)
print 'frozen kwds : {}'.format(rv2.kwds)
positional and keyword
frozen args : (3.14, 5.0, 2.0)
frozen kwds : {}
positional only
frozen args : (3.14,)
frozen kwds : {'loc': 5.0, 'scale': 2.0}
奖金:处理args
和kwds
的私有方法
有一个私有方法, .dist._parse_args()
,可以处理两种情况下的参数传递,并会返回一致的结果.
Bonus: Private method that handles both args
and kwds
There is an private method, .dist._parse_args()
, which handles both cases of parameter passing and will return a consistent result.
# Get the original parameters regardless of argument type
shape1, loc1, scale1 = rv1.dist._parse_args(*rv1.args, **rv1.kwds)
shape2, loc2, scale2 = rv2.dist._parse_args(*rv2.args, **rv2.kwds)
print 'positional and keyword'
print 'frozen parameters: shape={}, loc={}, scale={}'.format(shape1, loc1, scale1)
print
print 'positional only'
print 'frozen parameters: shape={}, loc={}, scale={}'.format(shape2, loc2, scale2)
positional and keyword
frozen parameters: shape=(3.14,), loc=5.0, scale=2.0
positional only
frozen parameters: shape=(3.14,), loc=5.0, scale=2.0
注意事项
允许使用私有方法通常是不好的做法,因为技术上内部的API总是可以更改的,但是,有时它们提供了不错的功能, Python中没有真正私有的东西:) .
Caveat
Granted, using private methods is typically bad practice because technically internal APIs can always change, however, sometimes they provide nice features, would be easy to re-implement should things change and nothing is really private in Python :).
这篇关于如何从冻结的Spicer.stats分布中获取参数自变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!