简体   繁体   English

获取 numpy.random 分布列表

[英]Getting list of numpy.random distributions

How can I get a get a list of the available numpy.random distributions as described in the docs ?如何获取文档中描述的可用numpy.random分布的列表?

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 (eg shuffle, get_state) which aren't distributions. ...但是这个列表包含额外的东西(例如shuffle,get_state),它们不是分布。

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.这是确保您不会获得将在未来版本的 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.他们很import numpy as np; print(np.random.__doc__)在模块文档中提供了列表( import numpy as np; print(np.random.__doc__) ),但是像你展示的那样遍历模块函数比解析文档字符串更安全。 They have defined the list ( np.random.__all__ ) which may be another interesting iterating possibility.他们定义了列表( np.random.__all__ ),这可能是另一个有趣的迭代可能性。

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.您的问题表明,应该审查 numpy 的命名约定,以包含类似性质的函数的前缀或将它们分组在子模块中。

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.编辑 2:Soravux 是正确的,这很糟糕,不太可能永远有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM