Suppose there are 1000 people and they are all asked if their favorite meal is breakfast, lunch, or dinner. I store that info in a list like:
mylist = [350, 450, 200]
i.e. 350 people like breakfast, 450 like lunch, 200 like dinner.
How do I randomly sample 100 people and get a similar list back? That is I want to get randomly sampled lists like
[35, 45, 20]
[33, 42, 25]
[37, 46, 17]
Thanks..
EDIT: I want to add one thing. The desired behavior is sampling without replacement. Suppose that, following the above example, I want to sample 999 people from the original 1000. It shouldn't be possible to get a list back that is, for instance, [350, 458, 201]
, because there aren't 201 people who like dinner.
This will do the job:
import numpy as np
res=np.random.choice(350*[0]+450*[1]+200*[2],size=100,replace=False)
np.histogram(res,range(4))[0]
And more generally:
import numpy as np
v=[350,450,200]
res=np.random.choice(np.repeat(range(len(v)),v),size=100,replace=False)
np.histogram(res,range(len(v)+1))[0]
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments