🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEnumpy

numpy Documentation

LOADING ENGINE...

np.ptp()

AI & DATA SCIENCE // np-ptp

np.ptp() (peak to peak) returns the range of values in an array — the difference between its maximum and minimum — either overall or along a specified axis.

Syntax

np.ptp(arr, axis=None)

Deep Dive Course

ptp() is simply a convenient shorthand for computing an array's maximum minus its minimum in a single call, avoiding two separate reductions and a subtraction written out by hand. Like other reduction functions, it accepts an axis argument to compute the range along a specific dimension of a multi-dimensional array rather than the flattened whole.

1Understanding np.ptp()

ptp() is simply a convenient shorthand for computing an array's maximum minus its minimum in a single call, avoiding two separate reductions and a subtraction written out by hand. Like other reduction functions, it accepts an axis argument to compute the range along a specific dimension of a multi-dimensional array rather than the flattened whole.

💡

np.ptp() is a small convenience over writing arr.max() minus arr.min() yourself — functionally identical, but slightly more direct and arguably clearer about the specific statistic being computed.

editor.html
import numpy as np

arr = np.array([3, 7, 1, 9, 4])
print(np.ptp(arr))
localhost:3000

2Practical Example

Here is a real-world application of np.ptp() showing how it is used in production NumPy code.

editor.html
import numpy as np

matrix = np.array([[1, 5, 3], [4, 2, 8]])
print(np.ptp(matrix, axis=0))
localhost:3000

3Best Practices

Follow these guidelines when working with np.ptp():

1. Use np.ptp() instead of manually writing arr.max() minus arr.min() for slightly clearer, more direct code

2. Use the axis argument to compute the range along a specific dimension of multi-dimensional data, such as the range of each column in a dataset

3. Be aware ptp() is sensitive to outliers, since it only depends on the two most extreme values — consider a percentile-based range if outlier resistance matters

⚠️

Tip: np.ptp() is a small convenience over writing arr.max() minus arr.min() yourself — functionally identical, but slightly more direct and arguably clearer about the specific statistic being computed.

editor.html
import numpy as np

arr = np.array([3, 7, 1, 9, 4])
print(np.ptp(arr))
localhost:3000

Examples

Example 01Basic Usage
import numpy as np

arr = np.array([3, 7, 1, 9, 4])
print(np.ptp(arr))
Example 02Advanced Example
import numpy as np

matrix = np.array([[1, 5, 3], [4, 2, 8]])
print(np.ptp(matrix, axis=0))

Best Practices

  • Use np.ptp() instead of manually writing arr.max() minus arr.min() for slightly clearer, more direct code
  • Use the axis argument to compute the range along a specific dimension of multi-dimensional data, such as the range of each column in a dataset
  • Be aware ptp() is sensitive to outliers, since it only depends on the two most extreme values — consider a percentile-based range if outlier resistance matters

Interview Question

Why is np.ptp() considered sensitive to outliers, compared to a measure like the interquartile range?

Hint: Think about exactly which values ptp() actually looks at.

ptp() only depends on the two most extreme values in the dataset, the maximum and the minimum — every other value in between is completely ignored. A single unusually large or small outlier directly becomes the new maximum or minimum, immediately and proportionally widening the reported range, even if every other value in the dataset is tightly clustered together. A measure like the interquartile range instead looks at percentile-based cutoffs that exclude the most extreme values entirely, making it far more robust to a small number of outliers.

Exercises

MediumPractice using np.ptp() in a real scenario.
View Solution
import numpy as np

arr = np.array([3, 7, 1, 9, 4])
print(np.ptp(arr))

Frequently Asked Questions

Why is np.ptp() considered sensitive to outliers, compared to a measure like the interquartile range?

ptp() only depends on the two most extreme values in the dataset, the maximum and the minimum — every other value in between is completely ignored. A single unusually large or small outlier directly becomes the new maximum or minimum, immediately and proportionally widening the reported range, even if every other value in the dataset is tightly clustered together. A measure like the interquartile range instead looks at percentile-based cutoffs that exclude the most extreme values entirely, making it far more robust to a small number of outliers.

Related Functions

np-minnp-maxnp-percentile