ndim
ndim counts how many dimensions (axes) an array has.
In this page:
Syntax
arr.ndim
ndim
ndim is 1 for a line of values, 2 for a table, 3 for a stack of tables. It always equals the length of the shape tuple. Knowing ndim helps you pick the right indexing and axis arguments.
Note:
A single number wrapped by np.array has ndim 0.
Example: ndim
import numpy as np
print(np.array(5).ndim)
print(np.array([1, 2, 3]).ndim)
print(np.array([[1, 2], [3, 4]]).ndim)
print(np.zeros((2, 3, 4)).ndim)
# Output:
# 0
# 1
# 2
# 3
Related Topics
Common Mistakes
- Confusing ndim with the length of the array
- Mixing up dimensions and rows
- Forgetting scalars have zero dimensions
Chapter Summary
- ndim is the number of axes
- It equals len(shape)
- Scalars have ndim 0
- It is an attribute not a method