NumPy ptp()

The numpy.ptp() method computes the range of values (maximum - minimum) in an array along the specified axis. Here, ptp stands for peak to peak.

Example

import numpy as np

# create an array
array1 = np.array([1, 2, 3, 4, 5, 6, 7])

# calculate the range of the array range = np.ptp(array1)
print(range) # Output: 6

ptp() Syntax

The syntax of the numpy.ptp() method is:

numpy.ptp(array, axis = None, out = None, keepdims = <no value>)

ptp() Arguments

The numpy.ptp() method takes the following arguments:

  • array - array containing numbers whose range is desired (can be array_like)
  • axis (optional) - axis or axes along which the range is computed (int or tuple of int)
  • out (optional) - the output array in which to place the result (ndarray)
  • keepdims (optional) - specifies whether to preserve the dimensions of the original array (bool)

Notes: The default values of ptp() arguments have the following implications:

  • axis = None, i.e. the range of the entire array is taken.
  • By default, keepdims will not be passed.

ptp() Return Value

The numpy.ptp() method returns the range of values (maximum - minimum) in an array along the specified axis.

Note: If one of the elements in the given axis is NaN, numpy.ptp() also returns NaN.


Example 1: Find the Range of a ndArray

import numpy as np

# create an array
array1 = np.array([[[0, 1], 
                    [2, 3]], 
                    [[4, 5], 
                    [6, 7]]])

# find the range of entire array range1 = np.ptp(array1) # find the range across axis 0 range2 = np.ptp(array1, 0) # find the range across axis 0 and 1 range3 = np.ptp(array1, (0, 1))
print('\nRange of the entire array:', range1) print('\nRange across axis 0:\n', range2) print('\nRange across axis 0 and 1', range3)

Output

Range of the entire array: 7

Range across axis 0:
[[4 4]
 [4 4]]

Range across axis 0 and 1 [6 6]

Example 2: Using Optional keepdims Argument

If keepdims is set to True, the resultant range array is of same number of dimensions as the original array.

import numpy as np

array1 = np.array([[1, 2, 3],
                [4, 5, 6]])

# keepdims defaults to False
result1 = np.ptp(array1, axis = 0)

# pass keepdims as True
result2 = np.ptp(array1, axis = 0, keepdims = True)

print('Dimensions in original array:', array1.ndim)
print('Without keepdims:', result1, 'with dimensions', result1.ndim)
print('With keepdims:', result2, 'with dimensions', result2.ndim)

Output

Dimensions in original array: 2
Without keepdims: [3 3 3] with dimensions 1
With keepdims: [[3 3 3]] with dimensions 2

Example 3: Using Optional out Argument

The out parameter allows us to specify an output array where the result will be stored.

import numpy as np

array1 = np.array([[12, 21, 13],
                [41, 15, 6]])

# create an output array
output = np.zeros(3)

# compute range and store the result in the output array np.ptp(array1, out = output, axis = 0)
print('Range:', output)

Output

Range: [29.  6.  7.]

Example 4: Data Type for ptp()

The numpy.ptp() method has no parameter to specify the data type of the output array where the result will be stored. This is because it will use the same data type as the element of the original array.

import numpy as np

array1 = np.array([[127, 127, 127],
                [1, 0, -1]], dtype = np.int8)

# compute range and store the result in the output array output = np.ptp(array1, axis = 0)
# print the range of the array print('Range:', output)
# print the data type of the output array print('Data Type of output array:', output.dtype)

Output

Range: [ 126  127 -128]
Data Type of output array: int8

In this example, the given data type is np.int8 which ranges from -128 to 127.

For the third column, the peak to peak value is 127 - (-1) = 128 which goes out of range for np.int8, thus giving us -128.

Note: A negative value can be returned when the input is an array of signed integers.