Reputation: 402
I have a numpy array like this:
arr = [.2 , .1, .2, .3, .2 ]
Now what I want to do is sample from this array, such that the probability i get a certain index is dependent on the probability at the index. So for example, the probability i get index 3 is .3
Anyone know any nifty ways to do this?
Upvotes: 7
Views: 6314
Reputation: 239
You can do weighted sampling with a discrete probability distribution using np.random.choice
by providing the sampling distribution as a parameter p
:
import numpy as np
x = [.2 , .1, .2, .3, .2 ]
# sample from `x` 100 times according to `x`
n_samples = 100
samples = np.random.choice(x, n_samples, p=x)
Upvotes: 6