How To Find The Shape Of An Array In Numpy Python
What is The Shape Of An Array In Numpy Python
The shape of an array is the number of elements in each dimension of the array.
For NumPy arrays there is an attribute called shape that returns a tuple with each index having the number of corresponding elements of the Numpy Array.
Example:
Let's look at an exmaple for finding the shape of an 2D Array
import numpy as np
arr = np.array([[5, 2, 9, 4], [9, 6, 11, 8]])
print(arr.shape)
Output:
(2,4)
Explanation:
The array is a 2 dimension array which contains 4 elements in each dimension of the array
so it is returning (2,4) as the output of the array
How to find the shape of a five dimension array in Numpy Python
First let;s create an array with 5 dimensions using ndmin using a vector with values 5,6,7,8 and verify that last dimension has value 4:
import numpy as np
arr = np.array([5, 6, 7, 8], ndmin=5)
print(arr)
print('shape of array :', arr.shape)
Output:
[[[[[5 6 7 8]]]]]
shape of array : (1, 1, 1, 1, 4)
What does the shape tuple represent?
Integers at every index tells about the number of elements the corresponding dimension has.
In the example above at index-4 we have value 4, so we can say that 5th ( 4 + 1 th) dimension has 4 elements.
Comments
Post a Comment
souvikdutta.aec@gmail.com