问题描述
我正在寻找一个简单的函数,它可以根据它们相应的(也指定的)概率生成一组指定的随机值.我只需要它来生成浮点值,但我不明白为什么它不能生成任何标量.我可以想出很多从现有函数构建它的方法,但我想我可能只是错过了一个明显的 SciPy 或 NumPy 函数.
例如:
>>>值 = [1.1, 2.2, 3.3]>>>概率 = [0.2, 0.5, 0.3]>>>打印 some_function(值,概率,大小 = 10)(2.2, 1.1, 3.3, 3.3, 2.2, 2.2, 1.1, 2.2, 3.3, 2.2)注意:我发现了 scipy.stats.rv_discrete 但我不明白它是如何工作的.具体来说,我不明白这(下面)是什么意思,也不明白它应该做什么:
numargs = generic.numargs[ <形状>] = ['用合理的值替换', ]*numargs
如果我应该使用 rv_discrete,能否请您提供一个简单的示例并解释上述形状"语句?
从离散分布中进行绘图直接内置到 numpy 中.该函数被称为 random.choice(难在 numpy 文档中没有任何参考离散分布的情况下找到).
elements = [1.1, 2.2, 3.3]概率 = [0.2, 0.5, 0.3]np.random.choice(元素,10,p=概率)
I am looking for a simple function that can generate an array of specified random values based on their corresponding (also specified) probabilities. I only need it to generate float values, but I don't see why it shouldn't be able to generate any scalar. I can think of many ways of building this from existing functions, but I think I probably just missed an obvious SciPy or NumPy function.
E.g.:
>>> values = [1.1, 2.2, 3.3]
>>> probabilities = [0.2, 0.5, 0.3]
>>> print some_function(values, probabilities, size=10)
(2.2, 1.1, 3.3, 3.3, 2.2, 2.2, 1.1, 2.2, 3.3, 2.2)
Note: I found scipy.stats.rv_discrete but I don't understand how it works. Specifically, I do not understand what this (below) means nor what it should do:
numargs = generic.numargs
[ <shape(s)> ] = ['Replace with resonable value', ]*numargs
If rv_discrete is what I should be using, could you please provide me with a simple example and an explanation of the above "shape" statement?
Drawing from a discrete distribution is directly built into numpy.The function is called random.choice (difficult to find without any reference to discrete distributions in the numpy docs).
elements = [1.1, 2.2, 3.3]
probabilities = [0.2, 0.5, 0.3]
np.random.choice(elements, 10, p=probabilities)
这篇关于使用 SciPy 或 NumPy 生成具有指定权重的离散随机变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!