Select one element from a list using python following the normal distribution

醉酒当歌 提交于 2021-02-07 10:30:45

问题


I would like to select one element from a list using python following the normal distribution. I have a list, e.g.,

alist = ['an', 'am', 'apple', 'cool', 'why']

For example, according to the probability density function (PDF) of normal distribution, the 3rd element in the given list should have the largest probability to be chosen.Any suggestions?


回答1:


from random import normalvariate

def normal_choice(lst, mean=None, stddev=None):
    if mean is None:
        # if mean is not specified, use center of list
        mean = (len(lst) - 1) / 2

    if stddev is None:
        # if stddev is not specified, let list be -3 .. +3 standard deviations
        stddev = len(lst) / 6

    while True:
        index = int(normalvariate(mean, stddev) + 0.5)
        if 0 <= index < len(lst):
            return lst[index]

then

alist = ['an', 'am', 'apple', 'cool', 'why']
for _ in range(20):
    print(normal_choice(alist))

gives

why
an
cool
cool
cool
apple
cool
apple
am
am
apple
apple
apple
why
cool
cool
cool
am
am
apple



回答2:


Are you sure you really want a normal distribution, you could look at a Beta Distribution, which would probably give you what you need, e.g.:

>>> import random
>>> from collections import Counter
>>> alist = ['an', 'am', 'apple', 'cool', 'why']
>>> Counter(alist[int(random.betavariate(2, 2)*len(alist))] for _ in range(100))
Counter({'am': 20, 'an': 9, 'apple': 34, 'cool': 23, 'why': 14})
>>> Counter(alist[int(random.betavariate(10, 10)*len(alist))] for _ in range(100))  
Counter({'am': 18, 'apple': 64, 'cool': 18}) 


来源:https://stackoverflow.com/questions/35472461/select-one-element-from-a-list-using-python-following-the-normal-distribution

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!