NumPy arrays offer the ndim attribute, which returns an integer indicating the number of dimensions in the array.
Determine the number of dimensions of the arrays.
import numpy as np a = np.array(42) b = np.array([1, 2, 3, 4, 5]) c = np.array([[1, 2, 3], [4, 5, 6]]) d = np.array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]]) print(a.ndim) print(b.ndim) print(c.ndim) print(d.ndim) |
An array can have any number of dimensions. When creating the array, you can specify the number of dimensions using the ndmin
argument.
Create an array with five dimensions and confirm that it indeed has five dimensions.
import numpy as np arr = np.array([1, 2, 3, 4], ndmin=5) print(arr) print(‘number of dimensions :’, arr.ndim) |