About [:, -1, :] [:, :, -1] [-1, :, :] in python

In fact, it is a relatively simple knowledge point, but because I often forget it, record it here

Take [:, -1, :] as an example

The colon represents all elements of the dimension

-1 represents the last element of the dimension

Such as

output = np.array([i for i in range(8)]).reshape(2, 2, 2)
print(output)

The output is

[[[0 1]
  [2 3]]

 [[4 5]
  [6 7]]]

And after processing [:, -1, :]

print(output[:, -1, :])

become

[[2 3]
 [6 7]]

[2 3] is the entire content of the last element in the second dimension

Note that this result actually reduces one dimension, and the result is a two-dimensional matrix

Similarly, we can look at the results of [:, :, -1] and [-1, :, :]

print(output[:, :, -1])
print(output[-1, :, :])

Respectively are

[[1 3]
 [5 7]]


[[4 5]
 [6 7]]

The result is actually reduced by one dimension, a two-dimensional matrix

Guess you like

Origin blog.csdn.net/weixin_39518984/article/details/109479717