Python:NumPy ndim
Published Nov 5, 2025
Contribute to Docs
The ndim attribute returns the total number of dimensions (axes) of a NumPy array. A 1D array acts like a list, a 2D array forms a matrix, and higher dimensions represent tensors which are common in data science and machine learning.
Syntax
ndarray.ndim
Parameters:
The ndim attribute takes no parameters.
Return value:
Returns an integer representing the number of array dimensions (axes).
Example: Checking Dimensions of Different Arrays
This example demonstrates how ndim behaves for 0D (scalar), 1D (list), and 2D (matrix) arrays:
import numpy as np# 0D array (scalar)arr_0d = np.array(50)print("0D array:", arr_0d, "| Dimensions:", arr_0d.ndim)# 1D array (list)arr_1d = np.array([1, 2, 3, 4, 5])print("1D array:", arr_1d, "| Dimensions:", arr_1d.ndim)# 2D array (matrix)arr_2d = np.array([[1, 2, 3], [4, 5, 6]])print("2D array:\n", arr_2d, "\nDimensions:", arr_2d.ndim)
The output of this code is:
0D array: 50 | Dimensions: 01D array: [1 2 3 4 5] | Dimensions: 12D array:[[1 2 3][4 5 6]]Dimensions: 2
Codebyte Example: Using ndim in a NumPy Operation
In this example, ndim is used to identify the number of dimensions in different types of image data like grayscale (2D) and color (3D):
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python:NumPy on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
- With Certificate
- Beginner Friendly.24 hours