Python NumPy clip() Function

NumPy clip() Function:

The numpy module of Python has a function named numpy.clip() that can be used to clip the values in an array. The interval will be passed to the clip() function, and values outside the interval will be clipped for the interval edges.

If we choose the interval [1, 2], values less than 1 become 1 and those more than 2 become 2.

It is identical to numpy.maximum(x_min, numpy.maximum(x, x_max)). It is, though, faster than np.maximum(). There is no need to do a check to ensure that x_min < x_max in numpy.clip() function.

Syntax:

numpy.clip(a, a_min, a_max, out=None)

Parameters

a: This is required. It is an array (array-like) whose elements are to be clipped.

a_min, a_max: This is optional. It specifies the minimum and maximum values for clipping values. Clipping is not necessary on the lower interval edge and upper interval edge. Clipping is not performed on the associated edge if None is set. None can only be one of a _min and a_max. Both are broadcast against “a”.

out:  This is optional. It is the location where the result will be saved. If this is given, it must have the same shape as the returned array.

Return Value: 

An array with the elements of “a”, but with values < a_min are replaced with a_min and those > a_max with a_max is returned.

NumPy clip() Function in Python

Example1

Approach:

  • Import numpy module using the import keyword.
  • Pass some random list as an argument to the array() function to create an array and apply the reshape() function by passing arguments rowsize and column size.
  • Store it in a variable.
  • Print the above-given array.
  • Pass the given array, minimum and maximum values as the argument to the clip() function of the numpy module to clip the given array.
  • Store it in another variable.
  • Print the above obtained clipped array.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Pass some random list as an argument to the array() function 
# to create an array and apply the reshape() function by 
# passing arguments rowsize and column size
# Store it in a variable.
gvn_arry = np.array([1, 3, 5, 7, 9, 11, 13, 15, 17]).reshape(3,3)
# Print the above given array.
print("The above given array is:")
print(gvn_arry)
print()
# Pass the given array, minimum and maximum values as the argument to the
# clip() function of the numpy module to clip the given array.
# Store it in another variable.
clip_arry = np.clip(gvn_arry, 5, 15)
# Print the above obtained clipped array.
print("The above obtained clipped array is:")
print(clip_arry)

Output:

The above given array is:
[[ 1 3 5]
[ 7 9 11]
[13 15 17]]

The above obtained clipped array is:
[[ 5 5 5]
[ 7 9 11]
[13 15 15]]

Example2

Here, the clip() method is used to replace any negative numbers with 0.

Approach:

  • Import numpy module using the import keyword.
  • Pass some random list as an argument to the array() function to create an array and apply the reshape() function by passing arguments rowsize and column size.
  • Store it in a variable.
  • Print the above-given array.
  • Pass the given array, a_min=0 and a_max=None as the argument to the clip() function of the numpy module to clip the given array. Here replace all negative numbers with 0.
  • Store it in another variable.
  • Print the above obtained clipped array.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Pass some random random list as an argument to the array() function 
# to create an array and apply the reshape() function by 
# passing arguments rowsize and column size
# Store it in a variable.
gvn_arry = np.array([1, -3, 5, -7, -9, -11, 13, 15, -17]).reshape(3,3)
# Print the above given array.
print("The above given array is:")
print(gvn_arry)
print()
# Pass the given array, a_min=0 and a_max=None as the argument to the
# clip() function of the numpy module to clip the given array.
# Here replace all negative numbers with 0.
# Store it in another variable.
clip_arry = np.clip(gvn_arry, a_min=0, a_max=None)
# Print the above obtained clipped array.
print("The above obtained clipped array is:")
print(clip_arry)

Output:

The above given array is:
[[ 1 -3 5]
 [ -7 -9 -11]
 [ 13 15 -17]]

The above obtained clipped array is:
[[ 1 0 5]
 [ 0 0 0]
 [13 15 0]]