How to check if a NumPy Array is multidimensional or 1D

avatar
Borislav Hadzhiev

Last updated: Apr 12, 2024
3 min

banner

# How to check if a NumPy Array is multidimensional or 1D

Use the ndim attribute on the NumPy array to check if it is multidimensional or one-dimensional.

The ndim attribute returns the number of array dimensions:

  • greater than 1 if the array is multidimensional.
  • equal to 1 if the array is one-dimensional.
main.py
import numpy as np arr1 = np.array([2, 4, 6, 8]) print(arr1.ndim) # ๐Ÿ‘‰๏ธ 1 print('-' * 50) if arr1.ndim == 1: # ๐Ÿ‘‡๏ธ this runs print('The array is one-dimensional') else: print('The array is multidimensional') print('-' * 50) arr2 = np.array([[1, 2, 3], [4, 5, 6]]) print(arr2.ndim) # ๐Ÿ‘‰๏ธ 2

check if numpy array is multidimensional or one dimensional

The code for this article is available on GitHub

The ndarray.ndim attribute returns the number of array dimensions.

main.py
import numpy as np arr1 = np.array([2, 4, 6, 8]) print(arr1.ndim) # ๐Ÿ‘‰๏ธ 1 arr2 = np.array([[1, 2, 3], [4, 5, 6]]) print(arr2.ndim) # ๐Ÿ‘‰๏ธ 2

If you need to check if the array is one-dimensional, check if the ndim attribute returns 1.

main.py
import numpy as np arr1 = np.array([2, 4, 6, 8]) print(arr1.ndim) # ๐Ÿ‘‰๏ธ 1 print('-' * 50) if arr1.ndim == 1: # ๐Ÿ‘‡๏ธ this runs print('The array is one-dimensional') else: print('The array is multidimensional')

If the attribute returns 1, then the array is one-dimensional.

Otherwise, it is at least two-dimensional.

If you need to check if the array is multidimensional, check if the ndim attribute returns a value greater than 1.

main.py
import numpy as np arr = np.array([[1, 2, 3], [4, 5, 6]]) print(arr.ndim) # ๐Ÿ‘‰๏ธ 2 if arr.ndim > 1: # ๐Ÿ‘‡๏ธ this runs print('The array is multidimensional') else: print('The array is one-dimensional')
The code for this article is available on GitHub

You can use the same approach to check if the NumPy array is two-dimensional.

main.py
import numpy as np arr = np.array([[1, 2, 3], [4, 5, 6]]) print(arr.ndim) # ๐Ÿ‘‰๏ธ 2 if arr.ndim == 2: # ๐Ÿ‘‡๏ธ this runs print('The array is two-dimensional') else: print('The array is NOT two-dimensional')

# Checking for empty dimensions with numpy.squeeze()

If your array might have empty dimensions, use the numpy.squeeze() method before accessing the ndim attribute.

main.py
import numpy as np arr = np.array([[1, 2, 3]]) print(arr.ndim) # ๐Ÿ‘‰๏ธ 2 arr = np.squeeze(arr) print(arr.ndim) # ๐Ÿ‘‰๏ธ 1

checking for empty dimensions before ndim

The code for this article is available on GitHub

The array in the example has an empty dimension.

main.py
import numpy as np arr = np.array([[1, 2, 3]]) print(arr) # ๐Ÿ‘‰๏ธ [[1 2 3]] print(arr.ndim) # ๐Ÿ‘‰๏ธ 2

The numpy.squeeze method removes the axes of length one from the supplied array.

main.py
import numpy as np arr = np.array([[1, 2, 3]]) print(arr) # ๐Ÿ‘‰๏ธ [[1 2 3]] print(arr.squeeze()) # ๐Ÿ‘‰๏ธ [1 2 3]

Accessing the ndim attribute after calling squeeze() produces a different result.

main.py
import numpy as np arr = np.array([[1, 2, 3]]) print(arr.ndim) # ๐Ÿ‘‰๏ธ 2 arr = np.squeeze(arr) print(arr.ndim) # ๐Ÿ‘‰๏ธ 1

checking for empty dimensions before ndim

The code for this article is available on GitHub

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev