How to get parameter arguments from a frozen spicy

2019-04-30 14:44发布

Frozen Distribution

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))

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?

1条回答
不美不萌又怎样
2楼-- · 2019-04-30 15:31

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}

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

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 :).

查看更多
登录 后发表回答