Question

I have a 2 column array, 1st column weights and the 2 nd column values which I am plotting using python. I would like to draw 20 samples from this weighted array, proportionate to their weights. Is there a python/numpy command which does that?

Was it helpful?

Solution

Try numpy.random.choice:

your_samples = numpy.random.choice(your_array[1], size=20, replace=False,
                                   p=your_array[0])

OTHER TIPS

You need to refine your problem statement better. For example, if your array has only 1 row, what do you expect. If your array has 20,000 rows what do you expect? ...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top