卡住分发
在scipy.stats
你可以创建一个 frozen distribution这允许为该实例永久设置分布的参数化(形状、位置和比例)。
例如,您可以使用 a
、loc
和 scale
参数创建 Gamma 分布 ( scipy.stats.gamma
) 并卡住 这样他们就不必在每次需要分发时都被传递。
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
中取回这些值?
最佳答案
访问rv
卡住参数
是的,用于创建卡住分布的参数在分布实例中可用。它们存储在 args
& kwds
attribute 中.这将取决于分发的实例是使用位置参数还是关键字参数创建的。
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
的私有(private)方法
有一个私有(private)方法,.dist._parse_args()
,它处理两种参数传递情况,并将返回一致的结果。
# 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
警告
诚然,使用私有(private)方法通常是不好的做法,因为从技术上讲,内部 API 总是会发生变化,但是,有时它们会提供不错的功能,would be easy to re-implement should things change和 nothing is really private in Python :) .
关于python - 如何从卡住的 spicy.stats 分布中获取参数参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37501075/