tmt*_*prt 5 python random parameters distribution scipy
在scipy.stats您可以创建一个冻结的分布,使分布的参数(形状,位置和规模),以该实例被永久设置.
例如,您可以创建一个伽玛分布(scipy.stats.gamma)以a,loc及scale参数和冻结他们,使他们不必在每次所需要的时间分布各地的传递做.
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()
Run Code Online (Sandbox Code Playgroud)
gamma stats: (array(11.280000000000001), array(12.56))
rv stats : (array(11.280000000000001), array(12.56))
Run Code Online (Sandbox Code Playgroud)
rv参数?由于这个特性很可能不会传递参数,有没有办法从冻结的分布中获取这些值rv,稍后呢?
rv冻结参数是的,用于创建冻结分发的参数在分发实例中可用。它们存储在args&kwds属性中。这将取决于分发的实例是使用位置参数还是关键字参数创建的。
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)
Run Code Online (Sandbox Code Playgroud)
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}
Run Code Online (Sandbox Code Playgroud)
args和的私有方法kwds有一个私有方法,.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)
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
诚然,使用私有方法通常是不好的做法,因为从技术上讲,内部 API 总是会发生变化,但是,有时它们提供了很好的功能,如果情况发生变化,很容易重新实现,而 Python 中没有任何东西是真正私有的 :)。
| 归档时间: |
|
| 查看次数: |
1287 次 |
| 最近记录: |