Question: I have two arrays. 

"a", a 2d numpy array.

```
import numpy.random as npr

a = array([[5,6,7,8,9],[10,11,12,14,15]])
array([[ 5,  6,  7,  8,  9],
       [10, 11, 12, 14, 15]])

```

"idx", a 3d numpy array constituting three index variants I want to use to index "a".

```
idx = npr.randint(5, size=(nsamp,shape(a)[0], shape(a)[1]))
array([[[1, 2, 1, 3, 4],
        [2, 0, 2, 0, 1]],

       [[0, 0, 3, 2, 0],
        [1, 3, 2, 0, 3]],

       [[2, 1, 0, 1, 4],
        [1, 1, 0, 1, 0]]])

```

Now I want to index "a" three times with the indices in "idx" to obtain an object as follows:

```
array([[[6, 7, 6, 8, 9],
        [12, 10, 12, 10, 11]],

       [[5, 5, 8, 7, 5],
        [11, 14, 12, 10, 14]],

       [[7, 6, 5, 6, 9],
        [11, 11, 10, 11, 10]]])

```

The naive "a[idx]" does not work. Any ideas as to how to do this? (I use Python 3.4 and numpy 1.9)

Answer: 
You can use [`choose`](http://docs.scipy.org/doc/numpy/reference/generated/numpy.choose.html) to make the selection from `a`:

```
>>> np.choose(idx, a.T[:,:,np.newaxis])
array([[[ 6,  7,  6,  8,  9],
        [12, 10, 12, 10, 11]],

       [[ 5,  5,  8,  7,  5],
        [11, 14, 12, 10, 14]],

       [[ 7,  6,  5,  6,  9],
        [11, 11, 10, 11, 10]]])

```

As you can see, `a` has to be reshaped from an array with shape `(2, 5)` to an array with shape `(5, 2, 1)` first. This is essentially so that it is broadcastable with `idx`, which has shape `(3, 2, 5)`.

(I learned this method from @immerrr's answer here: <https://stackoverflow.com/a/26225395/3923281>)
You can use [`take`](http://docs.scipy.org/doc/numpy/reference/generated/numpy.take.html#numpy.take) array method:

```
import numpy

a = numpy.array([[5,6,7,8,9],[10,11,12,14,15]])

idx = numpy.random.randint(5, size=(3, a.shape[0], a.shape[1]))

print a.take(idx)

```