python - How to get parameter arguments from a frozen spicy.stats distribution? -


frozen distribution

in scipy.stats can create frozen distribution allows parameterization (shape, location & scale) of distribution permanently set instance.

for example, can create gamma distribution (scipy.stats.gamma) a,loc , scale parameters , freeze them not have passed around every time distribution needed.

import scipy.stats stats  # parameters particular gamma distribution a, loc, scale = 3.14, 5.0, 2.0  # 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)  # specific, 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 parameters not passed around result of feature, there way values frozen distribution, rv, later on?

accessing rv frozen parameters

yes, parameters used create frozen distribution available within instance of distribution. stored within args & kwds attribute. dependent on if distribution's instance created positional arguments or keyword arguments.

import scipy.stats stats  # parameters 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)  # frozen parameters print 'positional , 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 , keyword frozen args : (3.14, 5.0, 2.0) frozen kwds : {}  positional frozen args : (3.14,) frozen kwds : {'loc': 5.0, 'scale': 2.0} 

bonus: private method handles both args , kwds

there private method, .dist._parse_args(), handles both cases of parameter passing , return consistent result.

# 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 , 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 , keyword frozen parameters: shape=(3.14,), loc=5.0, scale=2.0  positional frozen parameters: shape=(3.14,), loc=5.0, scale=2.0 

caveat

granted, using private methods typically bad practice because technically internal apis can change, however, provide nice features, would easy re-implement should things change , nothing really private in python :).


Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -