Python随机列表索引与概率

TL8*_*L84 3 python

我如何编写一个函数来为我提供列表元素的随机索引,但基于列表中的概率?

该列表看起来像这样,有 5 个元素。

a = [0.1, 0.2, 0.4, 0.2, 0.1]
Run Code Online (Sandbox Code Playgroud)

有没有简单快速的解决方案?谢谢

Wil*_*ill 7

这听起来像是Numpynumpy.random.choice()及其p参数的工作:

p : 1-D array-like, optional

The probabilities associated with each entry in a. If not given,
the sample assumes a uniform distribtion over all entries in a.
Run Code Online (Sandbox Code Playgroud)

因此,如果只有一个列表(其中一个元素既是每个元素的概率,也是要选择的元素本身,您可以这样做:

from numpy.random import choice

elementsAndProbabilities = [0.1, 0.2, 0.4, 0.2, 0.1]

randomElement = choice(elementsAndProbabilities, p=elementsAndProbabilities)
print randomElement
Run Code Online (Sandbox Code Playgroud)

如果你有一个元素列表和每个元素的概率列表(单独的),你可以这样做:

from numpy.random import choice

elements = ["first", "second", "third", "fourth", "fifth"]
probabilities = [0.1, 0.2, 0.4, 0.2, 0.1]    

randomElement = choice(elements, p=probabilities)
print randomElement
Run Code Online (Sandbox Code Playgroud)

现在,你说你想要索引,而不是元素,所以我们可以像这样得到索引:

from numpy.random import choice

probabilities = [0.1, 0.2, 0.4, 0.2, 0.1]

randomElement = choice(range(len(probabilities)), p=probabilities)
print randomElement
Run Code Online (Sandbox Code Playgroud)


kin*_*all 3

如果你有 NumPy 可能会更快,但如果没有,这里有一个纯 Python 解决方案。

from random import random

a = [0.1, 0.2, 0.4, 0.2, 0.1]

def randombin(bins):
    r = random()
    p = 0
    for i, v in enumerate(bins):
        p += v
        if r < p:
           return i
    # p may not equal exactly 1.0 due to floating-point rounding errors
    # so if we get here, just try again (the errors are small, so this
    # should not happen very often).  You could also just put it in the
    # last bin or pick a bin at random, depending on your tolerance for
    # small biases
    return randombin(bins)

print randombin(a)
Run Code Online (Sandbox Code Playgroud)