Numpy ndarray shape with 3 parameters Numpy ndarray shape with 3 parameters pandas pandas

Numpy ndarray shape with 3 parameters


No, the shapes are different, you have to pay attention to the square brackets:

>>> np.zeros((2, 1, 3))array([[[ 0.,  0.,  0.]],       [[ 0.,  0.,  0.]]])

versus:

>>> np.zeros((1, 2, 3))array([[[ 0.,  0.,  0.],        [ 0.,  0.,  0.]]])

as you can see, in the first call, we have two times a pair of square brackets for the second dimension, whereas in the latter, we only have one such pair.

The shape is also different:

>>> np.zeros((2, 1, 3)).shape(2, 1, 3)>>> np.zeros((1, 2, 3)).shape(1, 2, 3)

So in the former we have a list that contains two sublists. Each of these sublists contains one element: a list of three elements. In the latter we have a list with one element: a sublist with two elements, and these two elements are lists with three elements.

So a vanilla Python list equivalent would be:

[ [ [0, 0, 0] ], [ [0, 0, 0] ] ]

versus:

[ [ [0, 0, 0], [0, 0, 0] ] ]


dim=1 is just a dumb dimension, you can alway regard a matrix of 2x3 as a tensor of 1x2x3.

However, they are technically not the same thing. So you can see the bracket [] in your 2 output is not exactly the same, the place of [] for the dumb dimension is not at the same position.

For removing dumb dimension, use

arr = np.squeeze(arr)


As per numpy.zeros documentation, the first argument is a sequence or int representing the shape of the array.

If you look closely the nested square brackets differ in line with the shapes you've built.

This example might make it clearer:

np.zeros((2, 3, 4))array([[[ 0.,  0.,  0.,  0.],        [ 0.,  0.,  0.,  0.],        [ 0.,  0.,  0.,  0.]],       [[ 0.,  0.,  0.,  0.],        [ 0.,  0.,  0.,  0.],        [ 0.,  0.,  0.,  0.]]])