Return the number of dimensions of an array. Parameters ---------- a : array_like Input array. If it is not already an ndarray, a conversion is attempted. Returns ------- number_of_dimensions : int The number of dimensions in `a`. Scalars are
(a)
| 3519 | |
| 3520 | @array_function_dispatch(_ndim_dispatcher) |
| 3521 | def ndim(a): |
| 3522 | """ |
| 3523 | Return the number of dimensions of an array. |
| 3524 | |
| 3525 | Parameters |
| 3526 | ---------- |
| 3527 | a : array_like |
| 3528 | Input array. If it is not already an ndarray, a conversion is |
| 3529 | attempted. |
| 3530 | |
| 3531 | Returns |
| 3532 | ------- |
| 3533 | number_of_dimensions : int |
| 3534 | The number of dimensions in `a`. Scalars are zero-dimensional. |
| 3535 | |
| 3536 | See Also |
| 3537 | -------- |
| 3538 | ndarray.ndim : equivalent method |
| 3539 | shape : dimensions of array |
| 3540 | ndarray.shape : dimensions of array |
| 3541 | |
| 3542 | Examples |
| 3543 | -------- |
| 3544 | >>> import numpy as np |
| 3545 | >>> np.ndim([[1,2,3],[4,5,6]]) |
| 3546 | 2 |
| 3547 | >>> np.ndim(np.array([[1,2,3],[4,5,6]])) |
| 3548 | 2 |
| 3549 | >>> np.ndim(1) |
| 3550 | 0 |
| 3551 | |
| 3552 | """ |
| 3553 | try: |
| 3554 | return a.ndim |
| 3555 | except AttributeError: |
| 3556 | return asarray(a).ndim |
| 3557 | |
| 3558 | |
| 3559 | def _size_dispatcher(a, axis=None): |