NumPy ptp() Function



The NumPy ptp() function computes the range (peak-to-peak) of an array, which is defined as the difference between the maximum and minimum values. This function is particularly useful for understanding the spread of data in an array.

If all values in the array or along the specified axis are NaN, a RuntimeWarning is raised and the result will be NaN. Similarly, if the array contains positive or negative infinity values (inf or -inf), the function will still calculate the range considering infinities as part of the range calculation.

The ptp() function returns a float data type when any element in the input array is a float, even if all values are integers. This ensures compatibility with arrays that may contain NaN, infinities, or fractional numbers. If the input array contains only integers and no NaN values, the return type matches the input's data type.

Following is the syntax of the NumPy ptp() function −

numpy.ptp(a, axis=None, out=None, keepdims=False)  

Parameters

Following are the parameters of the NumPy ptp() function −

  • a: Input array. The array can be of any shape or data type and may include NaN values.
  • axis (optional): Axis along which to compute the range. If None, the range is computed over the flattened array.
  • out (optional): Alternate output array to place the result. It must have the same shape as the expected output.
  • keepdims (optional): If True, the reduced dimensions are retained as dimensions of size one in the output. Default is False.

Return Values

This function returns a scalar value or a NumPy array containing the range (peak-to-peak) values along the specified axis.

Example

Following is a basic example of finding the range (peak-to-peak) value in an array using the NumPy ptp() function −

import numpy as np  
# input array with NaN values  
array = np.array([3, 1, 7, 9, 24, 79])  
# finding the peak-to-peak value, ignoring NaN  
range_value = np.ptp(array)  
print("Peak-to-Peak Value :", range_value)  

Output

Following is the output of the above code −

Peak-to-Peak Value : 78  

Example: Range Along an Axis

The ptp() function can find the range values along a specified axis of a multi-dimensional array. In the following example, we have computed the range values along rows and columns −

import numpy as np  
# 2D input array with NaN values  
array = np.array([[3, 7, 19], [8, 4, 2], [6, 45, 9]])  
# range along rows (axis=1)  
range_along_rows = np.ptp(array, axis=1)  
print("Range along rows:", range_along_rows)  
# range along columns (axis=0)  
range_along_columns = np.ptp(array, axis=0)  
print("Range along columns:", range_along_columns)  

Output

Following is the output of the above code −

Range along rows: [16  6 39]
Range along columns: [ 5 41 17]

Example: Range with 'keepdims' Parameter

The keepdims parameter retains the reduced dimension as a size-one dimension in the output. This means that when we pass a multi-dimensional array and set this parameter to True, the reduced dimension's size is kept as 1, preserving the original dimensionality of the array. In the following example, we have demonstrated its use −

import numpy as np  
# 2D input array 
array = np.array([[3, 7, 5], [8, 4, 16], [6, 1, 9]])  
# range along columns with keepdims=True  
range_with_keepdims = np.ptp(array, axis=0, keepdims=True)  
print("Range with keepdims:\n", range_with_keepdims)  

Output

Following is the output of the above code −

Range with keepdims:
 [[ 5  6 11]]

Example: Graphical Representation of 'ptp()'

In the following example, we have visualized the range values along rows and columns of a 2D array with NaN values. To achieve this, we need to import the numpy and matplotlib.pyplot modules −

import numpy as np  
import matplotlib.pyplot as plt  

# 2D input array  
array = np.array([[3, 7, 5], [8, 4, 2], [6, 1, 9]])  
# range along rows  
range_rows = np.ptp(array, axis=1)  
# range along columns  
range_columns = np.ptp(array, axis=0)  

plt.plot(range(len(range_rows)), range_rows, label="Range along rows (ignoring NaN)")  
plt.plot(range(len(range_columns)), range_columns, label="Range along columns (ignoring NaN)")  
plt.title("Visualization of ptp() Results")  
plt.xlabel("Index")  
plt.ylabel("Range Value")  
plt.legend()  
plt.grid()  
plt.show()  

Output

The plot visualizes the range values along rows and columns of the array −

ptp Visualization
numpy_statistical_functions.htm
Advertisements