lundi 11 novembre 2019

Output of numpy.random.choice appears as if it hasn't taken the probability list into account

I have a list of items,

items = ['a', 'b', 'c', 'd', 'e']

and I'm trying to create another list of items such that probability of occurrence of each item is predefined. So I use numpy.random.choice.

from numpy.random import choice
from collections import Counter

total_count = 100000

items = ['a', 'b', 'c', 'd', 'e']
probability = [0.4, 0.3, 0.2, 0.05, 0.05]
rand_items = choice(items, total_count, probability)
items_counter = Counter(rand_items)

for item, count in items_counter.most_common():
    print(f"{item}: {100 * count / total_count:.1f}%")

Now the output for this code is something like this:

(venv) PS D:\Code\python> python .\random\random_selection.py
c: 20.2%
d: 20.1%
e: 19.9%
b: 19.9%
a: 19.8%

It's clearly not correct. How can I fix it?




Aucun commentaire:

Enregistrer un commentaire