Last updated: Apr 11, 2024
Reading timeยท4 min
NaN
valuesnumpy.max()
and numpy.min()
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.
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 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:
axis
along which to find the peaks.When the axis
argument is set to 1
, the range of each row is returned.
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]
When the axis
is set to 0
, the range of each column is returned.
NaN
valuesIf 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.
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.]
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.
RuntimeWarning
exception when only NaN
values are contained in the array.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.
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]
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.
You can learn more about the related topics by checking out the following tutorials: