7

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?

1

1 Answer 1

6

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.