Jack - 1 year ago 83
Python Question

# Indexing of 3d numpy arrays with 2d arrays

I am attempting to extract values from a 3d numpy array. At the moment I can perform the following operations:

newmesh.shape
(40,40,40)

newmesh[2,5,6]
6

However, if I try to index it with an array, the result is not as expected;

newmesh[np.array([2,5,6])].shape
(3, 42, 42)

I have tried using np.take, however it produces the following;

np.take(newmesh,np.array([2,5,6]))
[-1 -1 -1]

Any ideas why this is happening? My goal is to input a (n,3) array, where each row corresponds to a value of newmesh, i.e. inputting a (n,3) array would give back a 1d array of length n.

With idx as the (n,3) indexing array, one approach using linear-indexing would be with np.ravel_multi_index -

np.take(newmesh,np.ravel_multi_index(idx.T,newmesh.shape))

An approach with tuple formation would look like this -

newmesh[tuple(idx.T)]

If there are just three dimensions, you can even just use columnar slices for indexing into each dimension, like so -

newmesh[idx[:,0],idx[:,1],idx[:,2]]

Runtime test If anyone's interested in seeing the performance numbers associated with the listed approaches, here's a quick runtime test -

In [18]: newmesh = np.random.rand(40,40,40)

In [19]: idx = np.random.randint(0,40,(1000,3))

In [20]: %timeit np.take(newmesh,np.ravel_multi_index(idx.T,newmesh.shape))
10000 loops, best of 3: 22.5 µs per loop

In [21]: %timeit newmesh[tuple(idx.T)]
10000 loops, best of 3: 20.9 µs per loop

In [22]: %timeit newmesh[idx[:,0],idx[:,1],idx[:,2]]
100000 loops, best of 3: 17.2 µs per loop
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download