6

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

I'm writing a command-line utility which creates noise. I'd like to grab each available distribution, and get their required parameters to generate command-line options.

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 extra stuff (e.g. shuffle, get_state) which aren't distributions.

4
  • 1
    As the answers have said, the problem is that numpy really doesn't provide this information in a machine-readable way. Even if it did, it'd be a tough guessing game trying to figure out what parameters to pass to each function in an automated way. Just use the documentation to create your own table of what functions to call with which arguments. Commented Jul 9, 2013 at 2:18
  • @BrenBarn Good advice. I disagree about the parameter passing though. Since the idea was to generate command line arguments for my script, required parameters would be positional arguments to a main option, and optional parameters would be optional additional options. Commented Jul 9, 2013 at 11:39
  • The problem is, how do you know what values are appropriate to pass for those parameters? Commented Jul 9, 2013 at 18:55
  • The goal here is writing a command-line interface. For example, the --normal option would take two arguments. That puts the user in change of deciding what's meaningful. Commented Jul 9, 2013 at 19:15

2 Answers 2

2

Just as they did in the documentation, you must list them manually. It is the only way to be sure you won't get undesirable functions that will be added in future versions of numpy. If you don't care about future additions, you could filter out function names that aren't distributions.

They were kind enough to provide the list in the module documentation (import numpy as np; print(np.random.__doc__)), but iterating through the module functions as you showed is way safer than parsing the docstring. They have defined the list (np.random.__all__) which may be another interesting iterating possibility.

Your question shows that numpy's naming conventions should be reviewed to include a prefix to functions of similar nature or to group them within sub-modules.

Sign up to request clarification or add additional context in comments.

3 Comments

I think I should get more distributions as they're added in future versions of numpy. That'd be the biggest benefit of dynamically grabbing a list of distributions somehow, rather than relying on a static list.
The problem with getting new distributions automatically is guessing their interface: What will you be providing to these new functions? How many parameters? You would guess their return range too?
I'm going to go with a static list like you suggested. But see my comment on the main question - I think I should be able to get at the function prototype (.. or whatever it's called in Python) and pull out enough info to generate command-line options, assuming the user knows what to do with them.
0

probably a prettier 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: included headers by accident.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.