'What does the .shape represent in Python?
I wanted to know what this represents, meaning, what does this tell me:
len(array.shape)
the array being a random np.array
Thank you!
Solution 1:[1]
You're asking for the length of the shape of an array, meaning that you'll get the number of dimensions as a result. A 3D array will have a shape (x, y, z)
, meaning that len(array.shape)
will give you the result 3.
Solution 2:[2]
The array.shape returns the maximum value in form of (x,y) of the the array And on calculating the length of the array.shape you will be getting the dimensions of the array
import numpy as np
a=np.array([1,2,3])
print(len(a.shape))
print(a.shape)
The above code will give 1 as output since it is 1D array
import numpy as np
a=np.array([[1,2,3],[4,5,6]])
print(len(a.shape))
print(a.shape)
The above code will give 2 as output since it is 2D array.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | ozmanda |
Solution 2 | Javad Nikbakht |