简体   繁体   English

切片 numpy 3-d 阵列

[英]slicing numpy 3-d array

I have a 3d numpy array such as:我有一个 3d numpy 阵列,例如:

data = np.array([[[1, 2, 3, -999],
                  [5, 6, 7, 8],
                  [9, 10, 11, 12]],
             
                 [[10, 20, 30, -999],
                  [50, 60, 70, 80],
                  [90, 100, 110, 120]],
             
                 [[100, 200, 300, -999],
                  [500, 600, 700, 800],
                  [900, 1000, 1100, 1200]]])

Further i want to slice my array at certain random positions此外,我想在某些随机位置切片我的数组

pos = [[0, 1], [0, 2], [1, 0]]

to slice the array对数组进行切片

slices = [data[:, p[0], p[1]] for p in pos]

to yield屈服

[[2, 20, 200], [3, 30, 300], [5, 50, 500]]

What would be a faster way to perform the slicing step?执行切片步骤的更快方法是什么?

You can use numpy arrays instead for indexing:您可以使用 numpy arrays 代替索引:

pos = np.array([[0, 1], [0, 2], [1, 0]])
slices = data[:, pos[:, 0], pos[:, 1]].T

This is documented under integer array indexing .这记录在integer 数组索引下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM