Random Sampling with different probabilities

Question:

I’m pretty new to python and maybe this is a very silly/stupid question, but I’ve got a tremendous headache from thinking about this problem.

I got a set of data, for example integers, from which I want to extract a random subset, but every object has a different probability. How can I extract the subset in a way that respect the probability distribution of the data?

I suppose that np.random_sample gives to all samples the same priority, so its not what I’m looking for…

Asked By: Gionata Benelli

||

Answers:

numpy.random.choice has a p parameter that lets you set probabilities for the different objects.

Answered By: Hannes Ovrén

Numpy library is usually faster, but if you wish to use python’s random library

you can try:

random.choices(population, weights=None, *, cum_weights=None, k=1)

link

Answered By: Siddharth
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.