Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
538 views
in Technique[技术] by (71.8m points)

python - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...