How to do weighted random sample of categories in python How to do weighted random sample of categories in python python python

How to do weighted random sample of categories in python


This might do what you want:

numpy.array([.3,.4,.3]).cumsum().searchsorted(numpy.random.sample(5))


Since nobody used the numpy.random.choice function, here's one that will generate what you need in a single, compact line:

numpy.random.choice(['a','b','c'], size = 20, p = [0.3,0.4,0.3])


import numpyn = 1000pairs = [(.3, 'a'), (.3, 'b'), (.4, 'c')]probabilities = numpy.random.multinomial(n, zip(*pairs)[0])result = zip(probabilities, zip(*pairs)[1])# [(299, 'a'), (299, 'b'), (402, 'c')][x[0] * x[1] for x in result]# ['aaaaaaaaaa', 'bbbbbbbbbbbbbbbbbbb', 'cccccccccccccccccccc']

How exactly would you like to receive the results?