The numpy.random.choice method can generate a random sample without replacement if different elements should have different probabilities. However, when I test it with
import numpy
a = [0, 1, 2, 3, 4, 5]
p = [0.1, 0.3, 0.3, 0.1, 0.1, 0.1]
result = [0, 0, 0, 0, 0, 0]
N = 1000000
k = 3
for i in range(0, N):
temp = numpy.random.choice(a, k, False, p)
for j in temp:
result[j] += 1
for i in range(0, 6):
result[i] /= (N * k)
print(result)
the second and third elements only show up 25% of the time which is off by a lot. I tried different probability distributions (e.g., [0.1, 0.2, 0.3, 0.1, 0.1, 0.2]) and every time the result didn't match the expectation. Is there an issue with my code or is numpy really that inaccurate?
Aucun commentaire:
Enregistrer un commentaire