python-3.xnumpyn-dimensional

How to decide shape of a n-dimension array in numpy


How can i say shape of a n-dimension matrix in numpy for example

import numpy as np
a=np.array([[[1,2],[3,4]]])
print(a.shape)

output of these is (1,2,2) How can i say it without using a computer. Thanks for any help.


Solution

  • You have 3 opening brackets at the beginning, so the shape has 3 elements.

    The first shape element is 1, because the first opening bracket contains one element, ie. "[[1,2],[3,4]]".

    The second shape element is 2, because you have two elements on that level, "[1,2]" and "[3,4]".

    The third shape element is 2, because again you have two elements on that level "1" and "2" (as well as "3" and "4").