here the dimension of arr should be 5 but it is returning 3…can anyone explain me why this is happening?
arr5.ndim returns the number of dimensions in arr5 which is 3.
If you want to print number of elements in each dimensions, you can try:
but how arr5 has 3 dimensions instead of 5?
Help me understand, why you think it is 5.
arr5.shape will print: (4, 2, 4), which is a 3-tuple hence the dimension is 3.
dimension 1 has 4 elements. (Each of which is a 2-d array of size 2x4)
Now for each of these 2x4 arrays, 2 represents the dimension 2(number of rows) and 4(number of columns) represent dimension 3.
got it…so what i can do if i want
np.ndim to print 5 as result?
Create a 5 dim array
For e.g. just do something like.
arr = np.ones((1, 1, 1, 1, 1))
Apart from this do a small exercise. Try this:
print(np.ones((1)) ) print(np.ones((1, 1)) ) print(np.ones((1, 1, 1)) )
Compare the output along with the number of square brackets in each of the outputs. Also check ‘shape’ and ‘ndim’ for each of these.
Hope it helps.