The shape of an array indicates the number of elements in each of its dimensions.
NumPy arrays possess an attribute called shape that returns a tuple, with each index representing the number of corresponding elements in that dimension.
Display the shape of a 2-D array.
import numpy as np arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8]]) print(arr.shape) |
The example above returns (2, 4), indicating that the array has 2 dimensions, with the first dimension containing 2 elements and the second dimension containing 4 elements.
Create a 5-dimensional array using ndmin
with a vector containing the values 1, 2, 3, and 4, and confirm that the last dimension has a value of 4.
import numpy as np arr = np.array([1, 2, 3, 4], ndmin=5) print(arr) print(‘shape of array :’, arr.shape) |
The integers at each index indicate the number of elements in the corresponding dimension.
In the example above, the value at index 4 is 4, which means that the 5th dimension (4 + 1) contains 4 elements.