Dark Mode On/Off

# NumPy ptp() function

In this tutorial, we will cover the `numpy.ptp()` function of the NumPy library.

In the statistical function `numpy.ptp()`, "ptp" stands for peak to peak.

• This function is used to return a range of values along an axis.

• The range can be calculated using range= maximum_value - minimum_value.

### Syntax of `numpy.ptp()`:

The syntax required to use this function is as follows:

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

Parameters:

Below we have the description of the parameters accepted by this function:

• a
This parameter is used to indicate the input array.

• axis
This parameter is used to indicate the axis along which we want the range value. By default, the input array is flattened(that is working on all the axis). Here, axis = 0 means along the column and axis = 1 means working along the row.

• out
This is an optional parameter that is used to indicate an alternative array in which we want to store the result or the output of this function. The array must have the same dimensions as the expected output.

Returned Values:

This function returns the range of the array (it will return a scalar value if the axis is none) or an array with range of values along the specified axis.

## Example 1:

In the code snippet given below, we will take 1D Array with its last element as NaN and will check the result:

``````import numpy as np

input_arr = [1, 10, 7, 20, 11, np.nan]
print("The Input array is : ")
print(input_arr)
print("The Range of input array is : ")
print(np.ptp(input_arr))``````

The Input array is :
[1, 10, 7, 20, 11, nan]
The Range of input array is :
nan

Note: In case one of the value in the array is NaN then its range is also NaN.

Now, we will take a 1D Array with all numbers:

``````import numpy as np

input_arr = [1, 10, 7, 20,11,56,67]
print("The Input array is : ")
print(input_arr)
print("The Range of input array is : ")
print(np.ptp(input_arr))``````

The Input array is :
[1, 10, 7, 20, 11, 56, 67]
The Range of input array is :
66

## Example 2:

Now we will take another example where we will use different parameters of this function:

``````import numpy as np

inp = [[15, 18, 16, 63, 44], [19, 4, 29, 5, 20], [24, 4, 54, 6, 4,]]
print("\nThe Input array is:")
print(inp)

# The Range of the flattened array is calculated as:
print("\nThe Range of the array when the axis = None : ")
print(np.ptp(inp))

# The Range along the first axis where axis=0 means vertical
print("The Range of the array when the axis = 0 : ")
print(np.ptp(inp, axis = 0))

# Range along the second axis where axis=1 means horizontal
print("The Range of the array when the axis = 1: ")
print(np.ptp(inp, axis = 1))``````

The Input array is:
[[15, 18, 16, 63, 44], [19, 4, 29, 5, 20], [24, 4, 54, 6, 4]]

The Range of the array when the axis = None :
59
The Range of the array when the axis = 0 :
[ 9 14 38 58 40]
The Range of the array when the axis = 1:
[48 25 50]

## Summary

In this tutorial, we covered the `numpy.ptp()` statistical fucntion of Numpy library with its syntax, parameters, and returned values along with a few code examples.

Want to learn coding?
Try our new interactive courses.
Over 20,000+ students enrolled.