# What is the numpy.clip() Method

The numpy.clip() function “limits the values in an array between a specified minimum and maximum value”. Any value in the array below the minimum value will be replaced with the minimum value, and any value above the maximum value will be replaced with the maximum value.

### Syntax

``````numpy.clip(arr, a_min, a_max, out=None, *, where=True, casting='same_kind',
order='K', dtype=None, subok=True, signature=None, extobj=None)``````

### Parameters

1. arr: The input array.
2. a_min: The minimum value to clip the elements of the input array. It can be a scalar or an array with the same shape as a.
3. a_max: The maximum value to clip the elements of the input array. It can be a scalar or an array with the same shape as a.
4. out: An optional output array to store the result.

Other parameters like where, casting, order, dtype, subok, signature, and extobj are advanced options that control the operation’s performance.

### Example

``````import numpy as np

# Sample NumPy array
arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])
print("Original array:")
print(arr)

# Clip the values in the array between 3 and 7
clipped_arr = np.clip(arr, 3, 7)
print("\nClipped array:")
print(clipped_arr)
``````

Output

``````Original array:
[1 2 3 4 5 6 7 8 9]

Clipped array:
[3 3 3 4 5 6 7 7 7]``````

You can see that we created a sample NumPy array with values ranging from 1 to 9.

In the next step, we used the numpy.clip() function to limit the values in the array between 3 and 7. The resulting clipped array has all the values below 3 replaced with 3 and all above 7 replaced with 7.

This site uses Akismet to reduce spam. Learn how your comment data is processed.