Finding the Range of NumPy Array elements in Python

avatar
Borislav Hadzhiev

Last updated: Apr 11, 2024
4 min

banner

# Table of Contents

  1. Finding the Range of NumPy Array elements in Python
  2. Find the range of a NumPy array's elements and handle NaN values
  3. Find the range of a NumPy array's elements by using numpy.max() and numpy.min()

# Finding the Range of NumPy Array elements in Python

Use the numpy.ptp() method to find the range of a NumPy array's elements.

You can set the axis argument to 1 to find the range for each row.

If the axis argument is set to 0, the range for each column is returned.

main.py
import numpy as np arr = np.array([ [5, 1, 10], [3, 2, 6], [8, 2, 4], [5, 10, 1] ]) row_range = np.ptp(arr, axis=1) print(row_range) # ๐Ÿ‘‰๏ธ [9 4 6 9] column_range = np.ptp(arr, axis=0) print(column_range) # ๐Ÿ‘‰๏ธ [5 9 9]

find range of numpy array elements

The code for this article is available on GitHub

The range of a NumPy array's elements is calculated by subtracting the min value from the max value (max value - min value).

The row_range variable stores the range of each row and the column_range variable stores the range of each column.

The numpy.ptp() method returns the range of values (max - min) along the specified axis.

The method's name is an acronym for "peak to peak".

We passed the following 2 arguments to the numpy.ptp() method:

  1. The array-like object for which to find the ranges.
  2. The axis along which to find the peaks.

When the axis argument is set to 1, the range of each row is returned.

main.py
import numpy as np arr = np.array([ [5, 1, 10], [3, 2, 6], [8, 2, 4], [5, 10, 1] ]) row_range = np.ptp(arr, axis=1) print(row_range) # ๐Ÿ‘‰๏ธ [9 4 6 9] column_range = np.ptp(arr, axis=0) print(column_range) # ๐Ÿ‘‰๏ธ [5 9 9]
The code for this article is available on GitHub

When the axis is set to 0, the range of each column is returned.

# Find the range of a NumPy array's elements and handle NaN values

If you need to find the range of a NumPy array's elements and handle potential NaN values, use the numpy.nanmax and numpy.nanmin methods.

main.py
import numpy as np arr = np.array([ [5, 1, 10], [np.nan, 2, 6], [8, 2, np.nan], [5, 10, 1] ]) def get_range(array, axis): return np.nanmax(array, axis=axis) - np.nanmin(array, axis=axis) row_range = get_range(arr, axis=1) print(row_range) # ๐Ÿ‘‰๏ธ [9. 4. 6. 9.] column_range = get_range(arr, axis=0) print(column_range) # ๐Ÿ‘‰๏ธ [3. 9. 9.]

find range of numpy array elements and handle nan

The code for this article is available on GitHub

The numpy.nanmax() method returns the maximum of an array along the specified axis, ignoring any NaN values.

Conversely, the numpy.nanmin() method returns the minimum of an array along the specified axis, ignoring any NaN values.

Note that the methods raise a RuntimeWarning exception when only NaN values are contained in the array.

# Find the range of a NumPy array's elements by using numpy.max() and numpy.min()

You can also use the numpy.max() and numpy.min() methods to find the range of a NumPy array's elements.

main.py
import numpy as np arr = np.array([ [5, 1, 10], [3, 2, 6], [8, 2, 3], [5, 10, 1] ]) def get_range(array, axis): return np.max(array, axis=axis) - np.min(array, axis=axis) row_range = get_range(arr, axis=1) print(row_range) # ๐Ÿ‘‰๏ธ [9 4 6 9] column_range = get_range(arr, axis=0) print(column_range) # ๐Ÿ‘‰๏ธ [5 9 9]

find range of numpy array elements using numpy max min

The code for this article is available on GitHub

The numpy.max() method returns the maximum of an array along the given axis.

Conversely, the numpy.min() method returns the minimum of an array along the given axis.

Note that the methods don't ignore the NaN values.

If you need to ignore the NaN values, use the numpy.nanmax() and numpy.nanmin() methods as shown in the previous subheading.

# 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