Numpy ptp – Python NumPy ptp() Function

NumPy ptp() Function:

Numpy ptp: The ptp() function of the NumPy module returns an array’s range of values (maximum – minimum) or a range of values along a specified axis.

The function’s name is derived from the abbreviation for peak to peak.

Syntax:

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

Parameters

a: This is required. It is an array given as input.

axis: This is optional. It indicates the axis or axes along which we want the range value. The input array is flattened by default axis=None (that is working on all the axis). Axis = 0 denotes working along the column, while axis = 1 denotes working along the row.

out: This is optional. It specifies an alternate array in which the function’s result or output should be stored. The array’s dimensions must match those of the desired output.

keepdims: This is optional. The reduced axes are left in the outcome as dimensions with size one if this is set to True. The result will broadcast correctly against the input array if you use this option.

Return Value:

Numpy ptp: The ptp() function returns a range of “a” values. The output is a scalar value if the axis is None. If the axis is specified, an array of size a.ndim – 1 is returned.

NumPy ptp() Function in Python

Example1

np.ptp: Here, the array’s range of values is returned using the ptp() method.

Approach:

  • Import numpy module using the import keyword
  • Pass some random list as an argument to the array() function to create an array.
  • Store it in a variable.
  • Print the above given array.
  • Pass the given array as an argument to the ptp() function of numpy module to get the range of values(maximum-minimum) of the given array.
  • Store it in another variable.
  • Print the above result.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Pass some random list as an argument to the array() function to
# create an array. 
# Store it in a variable.
gvn_arry = np.array([[5,25],[35, 40]])              
# Print the above given array.
print("The above given array is:")
print(gvn_arry)
# Pass the given array as an argument to the ptp() function of numpy module 
# to get the range of values(maximum - minimum) of the given array 
# Store it in another variable.
rslt = np.ptp(gvn_arry)
# Print the above result
print("The range =", rslt)

Output:

The above given array is:
[[ 5 25]
 [35 40]]
The range = 35

Example2

Here, the ptp() function of numpy module is used to get the range of values(maximum – minimum) of the given array along axis=0

Approach:

  • Import numpy module using the import keyword
  • Pass some random list as an argument to the array() function to create an array.
  • Store it in a variable.
  • Print the above-given array.
  • Pass the given array, axis=0 as the arguments to the ptp() function of numpy module to get the range of values(maximum – minimum) of the given array along axis=0.
  • Store it in another variable.
  • Print the values range of the given array along axis=0.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Pass some random list as an argument to the array() function to
# create an array. 
# Store it in a variable.
gvn_arry = np.array([[5, 25, 50],[35, 40, 60]])              
# Print the above given array.
print("The above given array is:")
print(gvn_arry)
# Pass the given array, axis=0 as the arguments to the ptp() function of numpy module 
# to get the range of values(maximum - minimum) of the given array along axis=0 
# Store it in another variable.
rslt = np.ptp(gvn_arry, axis=0 )
# Print the values range of the given array along axis=0
print("The values range along axis=0 is: ", rslt)

Output:

The above given array is:
[[ 5 25 50]
 [35 40 60]]
The values range along axis=0 is: [30 15 10]

Example3

Here, the ptp() function of numpy module is used to get the range of values(maximum – minimum) of the given array along axis=1

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Pass some random list as an argument to the array() function to
# create an array. 
# Store it in a variable.
gvn_arry = np.array([[5, 25, 50],[35, 40, 60]])              
# Print the above given array.
print("The above given array is:")
print(gvn_arry)
# Pass the given array, axis=1 as the arguments to the ptp() function of numpy module 
# to get the range of values(maximum - minimum) of the given array along axis=1 
# Store it in another variable.
rslt = np.ptp(gvn_arry, axis=1 )
# Print the values range of the given array along axis=1
print("The values range along axis=1 is: ", rslt)

Output:

The above given array is:
[[ 5 25 50]
 [35 40 60]]
The values range along axis=1 is: [45 25]