How to create a random sample from a split population?

Suppose there are 1000 people and they are all asked if their favorite food is breakfast, lunch or dinner. I store this information in a list, for example:

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? That is, I want to get lists of random samples like

[35, 45, 20]
[33, 42, 25]
[37, 46, 17]

      

Thank..

EDIT: I want to add one. The desired behavior is fetch without replacement. Suppose, following the example above, I want to try 999 people out of the original 1000. Can't get a list that, for example [350, 458, 201]

, because there are no 201 people who like dinner.

+3


source to share


2 answers


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 in general:

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]

      

+4


source


One way to do this without replacement is to make random integers from 1 to 1000 (representing people) without replacement, and then determine which bucket they come in (breakfast, lunch, or dinner group):



import numpy

vec = [350, 450, 200]

# pick people without replacement
samp = numpy.random.choice(1000, 100, replace=False)

# get their preference
prefs = numpy.digitize(samp, numpy.cumsum(vec))

# count people by preference
numpy.histogram(prefs, 3)[0]

      

+2


source







All Articles