Numpy 3d array 从每一行中删除第一个条目

Numpy 3d array delete first entry from every row

假设我有一个 3d Numpy 数组:

array([[[0, 1, 2],
        [0, 1, 2],
        [0, 2, 5]]])

是否可以从所有行(最里面的那些行)中删除第一个条目。在这种情况下,每行中的 0 将被删除。

给我们以下输出:

[[[1, 2],
  [1, 2],
  [2, 5]]]
x
array([[[0, 1, 2],
        [0, 1, 2],
        [0, 2, 5]]])

x.shape
# (1, 3, 3)

您可以在所有最外层的轴上使用 Ellipsis (...) 到 select,并使用 1: 从每一行中切出第一个值。

x[..., 1:]    
array([[[1, 2],
        [1, 2],
        [2, 5]]])

x[..., 1:].shape
# (1, 3, 2)

补充@coldspeed的回复),slicing in numpy is very powerful and can be done in a variety of ways including with the colon operator : in the index,即

print(x[:,:,1:])
# array([[[1, 2],
#         [1, 2],
#         [2, 5]]])

相当于省略号的既定用法。