Python 通过索引列表对3D numpy数组进行切片

Python 通过索引列表对3D numpy数组进行切片,python,numpy,Python,Numpy,我看不出如何对数组进行切片,以便获得三维感兴趣的索引。下面是一个3D numpy阵列示例 data = np.arange(60).reshape(5,4,3) print data [[[ 0 1 2] [ 3 4 5] [ 6 7 8] [ 9 10 11]] [[12 13 14] [15 16 17] [18 19 20] [21 22 23]] [[24 25 26] [27 28 29] [30 31 32] [33 34

我看不出如何对数组进行切片,以便获得三维感兴趣的索引。下面是一个3D numpy阵列示例

data = np.arange(60).reshape(5,4,3)  
print data

[[[ 0  1  2]   [ 3  4  5]   [ 6  7  8]   [ 9 10 11]]

 [[12 13 14]   [15 16 17]   [18 19 20]   [21 22 23]]

 [[24 25 26]   [27 28 29]   [30 31 32]   [33 34 35]]

 [[36 37 38]   [39 40 41]   [42 43 44]   [45 46 47]]

 [[48 49 50]   [51 52 53]   [54 55 56]   [57 58 59]]]
下面是我想从三维空间中获取的索引

感兴趣的指数=np.random.randint(3,大小=5) 打印感兴趣的索引

[0 2 2 2 0]
所以基本上我想要价值观

[[[ 0] [ 3] [ 6] [ 9]]

[[14] [17] [20] [23]]

[[26] [29] [32] [35]]

[[38] [41] [44] [47]]

[[48] [51] [54] [57]]]
有没有办法做到这一点?当我尝试直接为数组编制索引时,它会广播维度,而不是向我提供数据的子集。

我们可以使用第三个维度来获取它们-

data[np.arange(len(indices_of_interest)),:, indices_of_interest]
样本运行-

In [65]: data = np.arange(60).reshape(5,4,3)

In [66]: indices_of_interest = [0,2,2,2,0]

In [67]: data[np.arange(len(indices_of_interest)),:, indices_of_interest]
Out[67]: 
array([[ 0,  3,  6,  9],
       [14, 17, 20, 23],
       [26, 29, 32, 35],
       [38, 41, 44, 47],
       [48, 51, 54, 57]])