## Numpy ptp Method

The range can be calculated as:

**Syntax of the NumPy ptp() Method**

NumPy ptp() method can be declared as:

**Parameters of the NumPy ptp() Method**

Now, we will discuss the description of the arguments accepted by the ptp() function:

**Arr**= Arr represents the data of the input array.

**Axis**= Axis represents that along which axis range would be found. By default, the input array work as flattened. Flattened means array work on all axes. If the value of the axis is 0, it represents the range along the column. And if the value of the axis is 1, it represents the range along the row.

**Out**= Out represents an alternative array in which we want to store the output or result. The dimensions of this array must match those of the desired result.

**Keep Dims**= It is also an optional argument. This parameter is helpful when the output array is incorrect or reduced to left with a dimension of size one, it will correct the results of the array.

**Return Value of the NumPy ptp() Method**

Return value means an output of the executed code. NumPy ptp() method will return the range of the array. It will return scalar values.

**Example # 1:**

In this example, we will discuss how to find or calculate a range of a 1D array using the NumPy ptp() function.

Let us start the code by importing the required library. We have to integrate a NumPy module of Python as np. Then, in the next statement, we initialized a one-dimensional array as ‘arr’ and assigned it different values. Then, we used the print() method to display the statement ‘Given array is’. Once again print() function is utilized to print the items of the given one-dimensional array. The statement ‘Range of given array is’ is printed by the use of the print() method. In the last step, NumPy ptp() method is applied to find the range of the provided array. To find the range, it is about minus the minimum value from the maximum value. The print statement is also declared to display the calculated range of the given 1D array.

We have an outcome in which we are left with a range of the given array.

**Example # 2:**

In this instance, we will see how to get a range of a 2D array by calling the NumPy ptp() function.

First, an important and mandatory step is to import a NumPy library of Python. We imported it as np. Next, we have taken ‘DATA’ as a variable and have assigned different values to this variable ‘DATA’. We have passed the two-dimensional array so that we acquire the range of that two-dimensional array. The values we have taken in 2D array are: [[2, 15], [10, 1]]. The print() method is declared to display the required elements of the 2D array as output. Again, we have called a print() function to show the statement ‘Range of given 2D array is’. Lastly we have called an np.ptp() function to find a range of the 2D array. This function contains the values of the provided 2d array as the parameter.

In the output, we have a range ‘14’ of the 2D array and it is calculated by: maximum value – minimum value.

**Example # 3:**

Here, we observe the method of computing the row-wise range of a 2D array by using the NumPy ptp().

As we already know, importing the library is the main step to perform. So, in this instance, for executing code, we have incorporated the NumPy module as np. Then, declared ‘X’ and it retained the elements of the two-dimensional array. Then, use a print() function to display the line ‘The given array is’. The print function also prints the two-dimensional array. Now, we will find the range of the given array by calling the NumPy ptp() method by providing the ‘axis’ parameter as axis = 1. It will give the range of the two-dimensional array row-wise.

In the outcome, we have the row-wise range of the 2D array as we have the value 1 of the parameter ‘axis’.

**Example # 4:**

Let us see how to acquire the column-wise range of a 2D array by the use of the NumPy ptp().

In this case, we will learn about finding the range of a 2D array vertically. The first step is integrating the NumPy library. The second step involves the initialization of the ‘Y’ variable as an input 2D array to store the values of the array. The third step is to print the values of the NumPy 2D array by calling the print() function by passing the values of ‘Y’ in it as an argument. In the fourth step, the print statement is again called to show the phrase ‘The Range of the given array when the axis = 0’. In the end, invoke the function np.ptp() to get the range of the defined 2D array. This method has two arguments, which include the required array and the parameter ‘axis’. Here, we set the value of the argument ‘axis’ to 0 because we want to find the range of the 2D array column-wise.

After the successful execution of the program, we have the following output:

**Conclusion**

To help you better understand the NumPy ptp() method, we have discussed a wide range of topics in the guide. The NumPy ptp() method’s syntax, parameters, and return value are all covered. The range of the one-dimensional array was computed in the first code, and the range of the two-dimensional array was determined in the second example. The range of the 2D array, both row- and column-wise has been evaluated in the last two instances.

Source: linuxhint.com