Getting the list of distributions numpy.random

How can I get a list of available numpy.random distributions as described in the docs ?

I am writing a command line utility that makes noise. I would like to capture every available distribution and get the necessary parameters for generating command line parameters.

I could almost do something like this:

 import numpy as np distributions = filter( lambda elt: not elt.startswith("__"), dir(np.random) ) 

... but this list contains additional materials (e.g. shuffle, get_state) that are not distributions.

+4
source share
2 answers

As in the documentation , you must list them manually. This is the only way to make sure you don't get unwanted features that will be added in future versions of numpy. If you do not need future additions, you can filter out the names of functions that are not distributions.

They were kind enough to provide a list in the module documentation ( import numpy as np; print(np.random.__doc__) ), but repetition through module functions, as you showed, is much safer than docstring analysis. They defined a list ( np.random.__all__ ), which could be another interesting iteration opportunity.

Your question shows that numpy naming conventions need to be revised to include a prefix in functions of a similar nature or to group them in submodules.

+1
source

perhaps in a more beautiful way, but:

 import numpy as np doc_string = np.random.__doc__ doc_string = doc_string.split("\n") distribs = [] for line in doc_string: if 'distribution' in line: word = line.split()[0] if word[0].islower(): distribs.append(word) 

gives

 >>> distribs ['beta', 'binomial', 'chisquare', 'exponential', 'f', 'gamma', 'geometric', 'gumbel', 'hypergeometric', 'laplace', 'logistic', 'lognormal', 'logseries', 'negative_binomial', 'noncentral_chisquare', 'noncentral_f', 'normal', 'pareto', 'poisson', 'power', 'rayleigh', 'triangular', 'uniform', 'vonmises', 'wald', 'weibull', 'zipf', 'dirichlet', 'multinomial', 'multivariate_normal', 'standard_cauchy', 'standard_exponential', 'standard_gamma', 'standard_normal', 'standard_t'] 

edit: headers are included randomly.

edit2: Soravux is right that this is bad and is unlikely to work forever.

0
source

Source: https://habr.com/ru/post/1490364/


All Articles