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…
Answers:
numpy.random.choice
has a p
parameter that lets you set probabilities for the different objects.
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)
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…
numpy.random.choice
has a p
parameter that lets you set probabilities for the different objects.
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)