What is Array Shape ?
The shape of a NumPy array is a tuple of integers that indicates:
-
number of rows
-
number of columns
-
number of higher dimensions (if any)
Syntax :
Python
array.shapeShape of a 1D Array?
A 1D array has only one dimension.
Python
import numpy as np arr = np.array([10, 20, 30, 40]) print(arr.shape) #output (4,) Shape of a 2D Array?
A 2D array has rows and columns. Python
import numpy as np arr = np.array([[1, 2, 3], [4, 5, 6]]) print(arr.shape) #output (2,3) Shape of a 3D Array?
A 3D array contains multiple 2D arrays. Python
import numpy as np arr = np.array([ [[1, 2], [3, 4]], [[5, 6], [7, 8]] ]) print(arr.shape) #output (2,2,2)Shape vs Size vs Dimensions?
| Attribute | Meaning |
|---|---|
shape | Structure of array |
size | Total number of elements |
ndim | Number of dimensions |