python - 如何从卡住的 spicy.stats 分布中获取参数参数?

标签 python random parameters scipy distribution

卡住分发

scipy.stats你可以创建一个 frozen distribution这允许为该实例永久设置分布的参数化(形状、位置和比例)。

例如,您可以使用 alocscale 参数创建 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}

奖励:处理 argskwds 的私有(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 changenothing is really private in Python :) .

关于python - 如何从卡住的 spicy.stats 分布中获取参数参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37501075/

相关文章:

python - 如何管理django中的锁写机制

Rcpp 中的 C++ 内置随机工件

c# - C# Generics 中的 "default"类型参数是否有合理的方法?

mysql - 我可以在 MySQL 中创建带参数的 View 吗?

sql-server-2008 - 在不同时间使用不同参数执行相同的 SSIS 包

python - 在python中解析.ini文件

python - 读取 xml 并尝试将其提取到 2 个不同的 xml 中

python - 在 Tornado 中将 Content-Length header 从服务器写入客户端

python - 在 Python 中生成随机字符

vector - 如何生成一个范围内的随机数向量?