Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

python - extract all vertical slices from numpy array

I want to extract a complete slice from a 3D numpy array using ndeumerate or something similar.

arr = np.random.rand(4, 3, 3)

I want to extract all possible arr[:, x, y] where x, y range from 0 to 2

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

ndindex is a convenient way of generating the indices corresponding to a shape:

In [33]: arr = np.arange(36).reshape(4,3,3)
In [34]: for xy in np.ndindex((3,3)):
    ...:     print(xy, arr[:,xy[0],xy[1]])
    ...:     
(0, 0) [ 0  9 18 27]
(0, 1) [ 1 10 19 28]
(0, 2) [ 2 11 20 29]
(1, 0) [ 3 12 21 30]
(1, 1) [ 4 13 22 31]
(1, 2) [ 5 14 23 32]
(2, 0) [ 6 15 24 33]
(2, 1) [ 7 16 25 34]
(2, 2) [ 8 17 26 35]

It uses nditer, but doesn't have any speed advantages over a nested pair of for loops.

In [35]: for x in range(3):
    ...:     for y in range(3):
    ...:         print((x,y), arr[:,x,y])

ndenumerate uses arr.flat as the iterator, but using it to

In [38]: for xy, _ in np.ndenumerate(arr[0,:,:]):
    ...:     print(xy, arr[:,xy[0],xy[1]])

does the same thing, iterating on the elements of a 3x3 subarray. As with ndindex it generates the indices. The element won't be the size 4 array that you want, so I ignored that.


A different approach is to flatten the later axes, transpose, and then just iterate on the (new) first axis:

In [43]: list(arr.reshape(4,-1).T)
Out[43]: 
[array([ 0,  9, 18, 27]),
 array([ 1, 10, 19, 28]),
 array([ 2, 11, 20, 29]),
 array([ 3, 12, 21, 30]),
 array([ 4, 13, 22, 31]),
 array([ 5, 14, 23, 32]),
 array([ 6, 15, 24, 33]),
 array([ 7, 16, 25, 34]),
 array([ 8, 17, 26, 35])]

or with the print as before:

In [45]: for a in arr.reshape(4,-1).T:print(a)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...