2

I'm a bit confused as why the following two slicing and index scenarios yield different shapes. Especially when x[:, [1], :, [1]] creates a shape of (1, 2, 2), as the slicing : at dim 0 should reserve the dimensions (2)? How was the shape determined in this case?

x = np.arange(2 * 2 * 2 * 2).reshape((2, 2, 2, 2))
print(f"x:\n{x}")
a = x[:, [1], [1], :]
print("================")
print(f"shape of x[:, [1], [1], :] == {a.shape}")
print(a)
print("================")
b = x[:, [1], :, [1]]
print(f"shape of x[:, [1], :, [1]] == {b.shape}")
print(b)

The results are

x:
[[[[ 0  1]
   [ 2  3]]

  [[ 4  5]
   [ 6  7]]]


 [[[ 8  9]
   [10 11]]

  [[12 13]
   [14 15]]]]
================
shape of x[:, [1], [1], :] == (2, 1, 2)
[[[ 6  7]]

 [[14 15]]]
================
shape of x[:, [1], :, [1]] == (1, 2, 2)
[[[ 5  7]
  [13 15]]]
Alan
  • 117
  • 2
  • 8

0 Answers0