How to Use NumPy clip() in Python

NumPy clip() function in Python is used to clip(limit) the elements in an array. In the clip() function, pass the interval(combination of minimum value and maximum value), values outside the interval are clipped to the interval edges. For example, if an interval of [1, 2] is specified, the values smaller than 1 are replaced with 1, and values larger than 2 are replaced with 2.

In this article, I will explain the Python NumPy clip() function syntax, parameters, and how it behave while clipping or trimming the NumPy array at a particular interval.

1. Quick Examples of Python NumPy Clip Function

If you are in a hurry, below are some quick examples of how to use the Python NumPy clip() function. For more information about NumPy refer NumPy Tutorial.


# Below are the quick examples

# Example 1: Use numpy.clip() function
arr = np.arange(15)
arr1 = np.clip(arr, 2, 10)

# Example 2: Use apply numpy.clip() on user defined array
arr1 = np.clip(arr, a_min = 8, a_max = 49)

# Example 3: When lower limit Of numpy clip is an array
arr1 = np.clip(arr, a_min = [6, 8, 11, 15, 10, 8, 8, 4, 4, 4,7,7], a_max = 52)

# Example 4: 
arr1 = np.zeros_like(arr)
arr2 = np.clip(arr, 7, 37,out = arr1)

# Example 5: 
arr = np.array([[1, 5, 8, 15], [29, 35, 38, 49]],np.uint16)

# Example 6: Set a_max = None
arr = [1, 5, 8, 15, 25, 29,35, 38,49,52,61,66]
arr1 = np.clip(arr, a_min = 8, a_max = None )

2. Syntax of NumPy clip()

Following is the syntax of clip() function.


# Syntax of clip() function
numpy.clip(arr, a_min, a_max, out=None)

2.1 Parameters of clip() Function

Following is the parameters of NumPy clip() function.

  • arr – An array which contains elements to be clipped.
  • a_min – It is a scalar or an array. If it is set to None, clipping is not performed on the lower interval edge. Not more than one of arr_min and arr_max may be None.
  • a_max – It is a scalar or an array. If it is set to None, clipping is not performed on the upper interval edge. Not more than one of a_min and a_max may be None.
  • out – The results will be placed in this array.

2.2 Return Value of clip()

It returns clipped array and the original array remains unchanged.

3. Usage of NumPy clip() Function

For clipping values in an array, the NumPy module of Python provides a function called numpy.clip(). When we specify the lower and upper limits of an array using this function, it will return an array where the elements less than the specified limit are replaced with the lowest limit, and greater than the specified limit are replaced with the highest limit.

Create NumPy array using arange() function then, we will apply clip() function and get the clipped array.


import numpy as np
arr = np.arange(15)

# Use numpy.clip() function
arr1 = np.clip(arr, 2, 10)
print("The clipped array:", arr1)

# Output
# The clipped array: [ 2  2  2  3  4  5  6  7  8  9 10 10 10 10 10]

4. Apply NumPy clip() on User Defined Array

Let’s create an array using numpy.array() function and apply clip() function along with the specified interval, it will return the clipped array. In this below arr object, all values less than 8 are replaced with 8, and greater than 49 are replaced with 49 values.


# Use apply numpy.clip() on user defined array
arr = [1, 5, 8, 15, 25, 29,35, 38,49,52,61,66]
arr1 = np.clip(arr, a_min = 8, a_max = 49)
print("The clipped array:", arr1)

# Output
# The clipped array: [ 8  8  8 15 25 29 35 38 49 49 49 49]

5. Set Lower limit is NumPy Array

We can set the lower limit with a scalar or an array for the clipped values of a given array. It will return the clipped array where the elements outside the interval are replaced with the specified limits. Let’s take an example,


# When Lower limit Of numpy clip is an array
arr1 = np.clip(arr, a_min = [6, 8, 11, 15, 10, 8, 8, 4, 4, 4,7,7], a_max = 52)
print(arr1)

# Output
# [ 6 8 11 15 25 29 35 38 49 52 52 52]

6. Set Higher Limit is None

Use None if you want to specify only one of the minimum and maximum values of the NumPy array. This function will return clipped array with the outer elements of replaced with the specified elements.


# Set a_max = None
arr = [1, 5, 8, 15, 25, 29,35, 38,49,52,61,66]
arr1 = np.clip(arr, a_min = 8, a_max = None )
print("The clipped array:", arr1)

# Output 
# The clipped array: [ 8  8  8 15 25 29 35 38 49 52 61 66]

If you try to set both minimum and maximum values are None, you will get Value Error. For example,


arr1 = np.clip(arr, a_min = None, a_max = None )
print("The clipped array:", arr1)

# Output
# ValueError: One of max or min must be given

7. Get the Clipped Array along Out Param

You want to output results to another array. Then in this case you have to pass the out parameters to the clip(). The out parameter should be the same dimension as the input NumPy array. You can see the results have been assigned to another array.


# output The clipped array to another array
arr1 = np.zeros_like(arr)
arr2 = np.clip(arr, 7, 37,out = arr1)
print(arr2)

# Output
# [ 7  7  8 15 25 29 35 37 37 37 37 37]

8. Conclusion

In this article, I have explained how to use Python NumPy clip() function and using this function along with specified interval how we can get the clipped array with examples.

Happy Learning!!

You May Also Like

References

NumPy clip

Leave a Reply

You are currently viewing How to Use NumPy clip() in Python