1

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.

1 Answer 1

1

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

2 Comments

Thanks this was very helpful. Do you understand why what I was doing was incorrect?
@Jack Well without any commas, in NumPy arrays, you are only indexing into the first dim with something like newmesh[np.array([2,5,6])], which would select three elems from first dim and everything from rest of the dims. So, it was essentially : newmesh[np.array([2,5,6]),:,:].

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.