user308827
user308827

Reputation: 21991

Sample from weighted histogram

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?

Upvotes: 2

Views: 1411

Answers (2)

Jaime
Jaime

Reputation: 67457

Try numpy.random.choice:

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

Upvotes: 6

Fred Mitchell
Fred Mitchell

Reputation: 2161

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? ...

Upvotes: -1

Related Questions