Numpy PTP Function

PythonPythonBeginner
Practice Now

Introduction

In this lab, you will learn about the numpy.ptp() function in Python. The "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.

VM Tips

After the VM startup is done, click the top left corner to switch to the Notebook tab to access Jupyter Notebook for practice.

Sometimes, you may need to wait a few seconds for Jupyter Notebook to finish loading. The validation of operations cannot be automated because of limitations in Jupyter Notebook.

If you face issues during learning, feel free to ask Labby. Provide feedback after the session, and we will promptly resolve the problem for you.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/DataScienceandMachineLearningGroup(["`Data Science and Machine Learning`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) numpy(("`NumPy`")) -.-> numpy/IndexingandSlicingGroup(["`Indexing and Slicing`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("`Numerical Computing`") python/FunctionsGroup -.-> python/build_in_functions("`Build-in Functions`") numpy/IndexingandSlicingGroup -.-> numpy/bool_idx("`Boolean Indexing`") numpy/IndexingandSlicingGroup -.-> numpy/fancy_idx("`Fancy Indexing`") subgraph Lab Skills python/comments -.-> lab-86491{{"`Numpy PTP Function`"}} python/lists -.-> lab-86491{{"`Numpy PTP Function`"}} python/tuples -.-> lab-86491{{"`Numpy PTP Function`"}} python/importing_modules -.-> lab-86491{{"`Numpy PTP Function`"}} python/numerical_computing -.-> lab-86491{{"`Numpy PTP Function`"}} python/build_in_functions -.-> lab-86491{{"`Numpy PTP Function`"}} numpy/bool_idx -.-> lab-86491{{"`Numpy PTP Function`"}} numpy/fancy_idx -.-> lab-86491{{"`Numpy PTP Function`"}} end

Importing NumPy Library

You need to import the NumPy library using the following code:

import numpy as np

Implementing the Function

Use the numpy.ptp() function to calculate the range of an array along an axis. The syntax of the function is:

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

Parameters

The function accepts the following parameters:

  • a: indicates the input array.
  • axis: indicates the axis along which we want the range value. By default, the input array is flattened (that is working on all the axis).
  • out: 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 the range of values along the specified axis.

Example 1

In this step, you will take a 1D array with its last element as NaN and will check the result. Use the following code:

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))

If an array has NaN as one of its values, then its range is also NaN.

Example 2

In this step, you will use different parameters of the function. Use the following code:

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))

Output

The output should show that the numpy.ptp() function returns the range of an array along an axis.

Summary

In this lab, you learned about the numpy.ptp() function in Python. You also learned about its syntax, parameters, and returned values. Additionally, you have used different examples of this function to understand it better. The numpy.ptp() function is an essential tool for statistical operations in NumPy library.

Other Python Tutorials you may like