NumPy | ptp method
Start your free 7-days trial now!
ptp(~) method returns the range (i.e. maximum - minimum) along the specified axis. Note that ptp stands for "peak to peak".
The input array.
The axis along which to compute the range. For 2D arrays, the allowed values are as follows:
Compute the range column-wise
Compute the range row-wise
Compute the range on a flattened array
Numpy array |
Instead of creating a new array, you can place the computed result into the array specified by
q is a scalar, then a scalar is returned. Otherwise, a Numpy array is returned.
Computing the range of a 1D array
To compute the range of a 1D array:
Computing the range of a 2D array
Suppose we have the following 2D array:
a = np.array([[5,6],[7,8]])aarray([[5, 6],[7, 8]])
To compute the range of the flattened version of
To compute the range column-wise:
np.ptp(a, axis=0)array([2, 2])
To compute the range row-wise:
np.ptp(a, axis=1)array([1, 1])