0

consider the following code :

test_1 = np.zeros((2, 2))
test_2 = np.zeros((2, 2, 1))
print(test_1[..., np.newaxis])
print(test_1[np.newaxis])

Result :

[[[0.]
  [0.]]

 [[0.]
  [0.]]]
[[[0. 0.]
  [0. 0.]]]

I really don't understand the meaning of this '...' and why the two lines don't produce a similar array. Do you have an idea ? Thanks a lot !

Joe Ferndz
  • 8,417
  • 2
  • 13
  • 33
  • `...` means "all existing dimensions", `np.newaxis`/`None` adds a new one – mozway Nov 02 '22 at 17:50
  • One adds the new dimension at the end, making a (2,2,1). The other adds it at the beginning, making a (1,2,2). In a case like this, displaying the `shape` instead of the array may be more instructive - though deducing the shape from the display is a good exercise. – hpaulj Nov 02 '22 at 20:44

0 Answers0