Numpy Clip can also be defined as clipping or trimming the numpy array at a particular interval. You can connect it with rounding off techniques in mathematics, which consists of the floor and ceiling values. You can learn more about Python Round off Techniques in our tutorials section.
What numpy.clip Do in Python
The primary purpose of the numpy clip function is to limit the upper value and lower value. And if the lower or upper value surpasses there outlined limit, rounding off the value to the maximum defined interval or interval. Let’s see it through a layman example.
For example: If an interval of [2, 5] is determined then, all the values lower than two become 2, and all the values higher than 5 become 5.
It is equivalent to the combination of np.minimum() and np.maximum() but more precise and faster than them.
np.minimum(a_max, np.maximum(a, a_min))
Syntax of Numpy Clip
numpy.clip(a, a_min, a_max, out=None)
|Parameter||Mandatory or Not|
- a: [arrayLike]
It consists of an array containing elements to clip.
- a_min: [scalar or array_like or None]
The lower value of the defined interval. If a_min is None, clipping is not performed on the lower interval edge. Not more than one of a_min and a_max may be None.
- a_max: [scalar or array_like or None]
The lower value of the defined interval. If a_max is None, clipping is not performed on the upper interval edge. Not more than one of a_min and a_max may be None.
- out: [ndarray, optional]
The results will be placed in this array. This parameter allows you to avoid unnecessary assignments, which gives a small gain in speed. Out must be of the right shape to hold the result. Its type is preserved.
Return Value of Numpy Clip
The Numpy Clip function returns the clipped array as the return value.
An array with the elements of a, but where values < a_min are replaced with a_min, and that> a_max with a_max.
Note: If a_min or a_max are array_like, then the three arrays will be broadcasted to match their shapes.
Examples of Numpy Clip Function
Let’s go through the examples of Numpy clip() function and see how it works.
Example 1: Using np.arrange() function to generate array
import numpy as np a = np.arange(15) print ("The input array : ", a) out = np.clip(a, 2, 13) print("The clipped array:", out)
The input array : [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14] The clipped array: [ 2 2 2 3 4 5 6 7 8 9 10 11 12 13 13]
In the above example we have first imported the numpy module as np, with the help of import keyword. After that we have used np.arrange() to get the array of numbers from [0-14]. If you don’t know what np.arrange do, then let me explain it to you. So numpy arange() is one of the array creation routines based on numerical ranges. It creates an instance of ndarray with evenly spaced values and returns the reference to it.
After creating the array with the help of the np.arrange() function we have printed it to check what will be our input string. Now comes the important part which is all about the Python Clip function. So what we have done is, we used the np.clip() function to limit the lower interval and higher interval.
Here in our example, we have used three mandatory parameters which are array, a_min, and a_max. a is the input array that we have generated through the numpy.arrange() function, a_min = 2 and a_max = 13. So, now the lower limit will be ‘2’ and the higher limit will be ’13’. All the numerals whose value is less than 2 will be clip to 2 and the element whose value will be greater than 13 will be clipped down to 13.
Example 2: applying numpy clip on user defined array
import numpy as np a = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21] print ("Input array : ",a) out = np.clip(a, a_min = 4, a_max = 12) print ("Array after clipping : ", out)
Input array : [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21] Array after clipping : [ 4 4 5 7 9 11 12 12 12 12 12]
So, in this example instead of using the numpy.arrange() function we have used our user-defined or pre-defined array. Everything in this example is similar except two things. (i) we have used a pre-defined array.
(ii) If you look closely we have used the value of a_min = 4 and a_max = 12, but they are not in the input array. But the numpy.clip() is a smart function and it automatically decides inside its an algorithm to clip according to the lower and upper limits.
Example 3: When Lower limit of numpy clip is an array
import numpy as np in_array = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21] print ("Input array : ", in_array) out_array = np.clip(in_array, a_min =[3, 2, 5, 5, 1, 9, 4, 5, 6, 2, 3], a_max = 9) print ("Output array : ", out_array)
Input array : [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21] Output array : [3 3 5 7 9 9 9 9 9 9 9]
Example 3, instead of using a numeral value as the value of a_min, we have used a pre-defined array. So what will happen here is for each value in the input array; a_min will check it parallelly. For example, the first value of the input array is ‘1’ will be matched with the parallel value in the a_min, which is ‘3’. In this case, ‘1’ is smaller than ‘3’. So the value of the input array element will be clipped to ‘3’. This approach will be followed until we cut the last element of the array with the numpy clip() function.
Example 4: When Upper Limit Of Numpy Clip Is An Array
import numpy as np in_array = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21] print ("Input array : ", in_array) out_array = np.clip(in_array, a_min = 3, a_max = [7, 5, 11, 9, 13, 20, 4, 9, 16, 8, 10]) print ("Output array : ", out_array)
Input array : [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21] Output array : [ 3 3 5 7 9 11 4 9 16 8 10]
This example is vice-versa of example 4. In this example, in place of the a_min, we build a_max as a pre-defined array.
Example 5: When both lower Limit and upper limit Of Numpy Clip are Array’s
import numpy as np in_array = [1, 3, 5, 7, 9] print ("Input array : ", in_array) out_array = np.clip(in_array, a_min = [2,2,2,2,2], a_max = [7, 5, 11, 9, 13]) print ("Output array : ", out_array)
Input array : [1, 3, 5, 7, 9] Output array : [2 3 5 7 9]
This is a unique type of example. Here we have made all three (input_array, a_min, a_max)as a pre-defined array. The input array will have to check the interval limit for each element of the array parallelly. Other things will go similar to example 3 & 4. You can say this is the concatenation of examples 3 & 4.
Numpy Clip for 2D Array
Numpy Clip can also be applied on an N-dimensional array. Especially for 2D arrays, which can be important for storing pixel values of the image, clipping can help you to eliminate garbage values. Mostly, the clip transverses through all the nested arrays, and allies clip to each of them. The following example can help you to understand it –
import numpy as np arr = np.array([[1, 20], [40, 8]],np.uint16) arr = arr.clip(6,10) print(arr)
[[ 6 10] [10 8]]
First, we start by importing the Numpy module. Then we declare the 2D numpy array with different values. When arr.clip(6, 10) is called, it truncates the values of all elements inside the array to lower and higher limits. In our case, it’ll be between 6 and 10 (both included).
What Will Happen if the Shape of Input Array and the Shape of Lower Limit (a_min)or Upper Limit (a_max) Arrays are Different
If the shape of the input array and any of the upper clipping limit or the lower clipping limit arrays are different than we will get a value error.
import numpy as np in_array = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21] print ("Input array : ", in_array) out_array = np.clip(in_array, a_min =[3, 2, 5, 5, 1, 9], a_max = 9) print ("Output array : ", out_array)
ValueError: operands could not be broadcast together with shapes (11,) (6,)
So, here we get a value error as the shape of the input array is 11 but the shape of a_min is 6. So we need to keep this in mind that the shape of arrays should be the same to avoid any kind of value error in the numpy clip function.
NumPy is very powerful and incredibly essential for information science in Python. That being true, if you are interested in data science in Python, you really ought to find out more about Python.
You might like our following tutorials on numpy.
- Numpy Mean: Implementation and Importance
- Using Numpy Random Function to Create Random Data
- NumPy Reshape: Reshaping Arrays With Ease
- In-depth Explanation of np.power() With Examples
The numpy clip() is a compelling and essential function available in the numpy module, which can be very useful and highly recommended by many experts while trimming the large datasets.
If you still have any questions regarding NumPy clip function?
Leave your question in the comments below.