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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With