# Sample from a 2d probability numpy array?

## Question:

Say that I have an 2d array `ar`

like this:

```
0.9, 0.1, 0.3
0.4, 0.5, 0.1
0.5, 0.8, 0.5
```

And I want to sample from [1, 0] according to this probability array.

```
rdchoice = lambda x: numpy.random.choice([1, 0], p=[x, 1-x])
```

I have tried two methods:

1) reshape it into a 1d array first and use `numpy.random.choice`

and then reshape it back to 2d:

```
np.array(list(map(rdchoice, ar.reshape((-1,))))).reshape(ar.shape)
```

2) use the vectorize function.

```
func = numpy.vectorize(rdchoice)
func(ar)
```

But these two ways are all too slow, and I learned that the nature of the vectorize is a for-loop and in my experiments, I found that `map`

is no faster than `vectorize`

.

I thought this can be done faster. If the 2d array is large it would be unbearably slow.

## Answers:

You should be able to do this like so:

```
>>> p = np.array([[0.9, 0.1, 0.3], [0.4, 0.5, 0.1], [0.5, 0.8, 0.5]])
>>> (np.random.rand(*p.shape) < p).astype(int)
```

Actually I can use the np.random.binomial:

```
import numpy as np
p = [[0.9, 0.1, 0.3],
[0.4, 0.5, 0.1],
[0.5, 0.8, 0.5]]
np.random.binomial(1, p)
```