🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEnumpy

numpy Documentation

LOADING ENGINE...

ndarray.ndim

AI & DATA SCIENCE // ndarray-ndim

ndarray.ndim is the number of dimensions (axes) the array has — 1 for a plain vector, 2 for a matrix, 3 or more for higher-dimensional data.

Syntax

arr.ndim

Deep Dive Course

ndim is simply the length of the array's shape tuple: a 1D array, a plain vector, has ndim 1, a 2D array, a matrix of rows and columns, has ndim 2, and higher-dimensional arrays, common in image or video data (height, width, color channels, and sometimes a batch dimension), have correspondingly higher ndim values. A 0-dimensional array, holding just a single scalar value wrapped in the ndarray type, has ndim 0.

1Understanding ndarray.ndim

ndim is simply the length of the array's shape tuple: a 1D array, a plain vector, has ndim 1, a 2D array, a matrix of rows and columns, has ndim 2, and higher-dimensional arrays, common in image or video data (height, width, color channels, and sometimes a batch dimension), have correspondingly higher ndim values. A 0-dimensional array, holding just a single scalar value wrapped in the ndarray type, has ndim 0.

💡

A common bug source is accidentally ending up with an extra dimension of size 1, a shape like (5, 1) instead of (5,), after some operation — checking .ndim is a quick way to catch this before it causes confusing broadcasting behavior downstream.

editor.html
import numpy as np

vector = np.array([1, 2, 3])
matrix = np.array([[1, 2], [3, 4]])
print(vector.ndim, matrix.ndim)
localhost:3000

2Practical Example

Here is a real-world application of ndarray.ndim showing how it is used in production NumPy code.

editor.html
import numpy as np

image_batch = np.zeros((10, 64, 64, 3))
print(image_batch.ndim)
print(image_batch.shape)
localhost:3000

3Best Practices

Follow these guidelines when working with ndarray.ndim:

1. Check .ndim when debugging unexpected shapes, especially after operations like slicing or reductions that can add or remove dimensions unexpectedly

2. Use np.squeeze() to remove unwanted size-1 dimensions when .ndim is higher than you actually intend

3. Be explicit with axis arguments in reduction functions instead of relying on default behavior that can silently reduce dimensionality

⚠️

Tip: A common bug source is accidentally ending up with an extra dimension of size 1, a shape like (5, 1) instead of (5,), after some operation — checking .ndim is a quick way to catch this before it causes confusing broadcasting behavior downstream.

editor.html
import numpy as np

vector = np.array([1, 2, 3])
matrix = np.array([[1, 2], [3, 4]])
print(vector.ndim, matrix.ndim)
localhost:3000

Examples

Example 01Basic Usage
import numpy as np

vector = np.array([1, 2, 3])
matrix = np.array([[1, 2], [3, 4]])
print(vector.ndim, matrix.ndim)
Example 02Advanced Example
import numpy as np

image_batch = np.zeros((10, 64, 64, 3))
print(image_batch.ndim)
print(image_batch.shape)

Best Practices

  • Check .ndim when debugging unexpected shapes, especially after operations like slicing or reductions that can add or remove dimensions unexpectedly
  • Use np.squeeze() to remove unwanted size-1 dimensions when .ndim is higher than you actually intend
  • Be explicit with axis arguments in reduction functions instead of relying on default behavior that can silently reduce dimensionality

Interview Question

What does it mean for a NumPy array to have ndim equal to 0, and how is that different from a plain Python number?

Hint: Think about a 0-dimensional array as a scalar wrapped in the ndarray type.

A 0-dimensional array holds exactly one value, with an empty shape tuple, and ndim equal to 0 — it represents a scalar, but as a full ndarray object rather than a plain Python int or float. This distinction matters because a 0-d array still carries NumPy-specific behavior, like a fixed dtype and support for NumPy's broadcasting and universal functions, which a plain Python number wouldn't have, even though both conceptually represent 'just one number'.

Exercises

MediumPractice using ndarray.ndim in a real scenario.
View Solution
import numpy as np

vector = np.array([1, 2, 3])
matrix = np.array([[1, 2], [3, 4]])
print(vector.ndim, matrix.ndim)

Frequently Asked Questions

What does it mean for a NumPy array to have ndim equal to 0, and how is that different from a plain Python number?

A 0-dimensional array holds exactly one value, with an empty shape tuple, and ndim equal to 0 — it represents a scalar, but as a full ndarray object rather than a plain Python int or float. This distinction matters because a 0-d array still carries NumPy-specific behavior, like a fixed dtype and support for NumPy's broadcasting and universal functions, which a plain Python number wouldn't have, even though both conceptually represent 'just one number'.

Related Functions

ndarray-shapendarray-sizenp-reshape