NumPy arrays are essential to most data analysis and data science workflows. You may want to limit arrays to contain values within a minimum and maximum range – this is what the NumPy clip function accomplishes. In this tutorial, you’ll learn how to limit the values of a NumPy array by using the
.clip() function. The function allows you to overwrite any outlier values.
By the end of this tutorial, you’ll have learned:
- How the NumPy
- How to use the NumPy clip function to limit the minimum and maximum values
- How to clip NumPy arrays in-place
Understanding the NumPy clip Function
The NumPy clip() function will clip, or limit, the values in an array. This allows you to set a lower and/or upper limit on the array. Any value outside of the interval are clipped to the interval edges.
Before diving into how to use the NumPy
.clip() function, let’s take a look at how the function is structured. It’s important to understand what the available parameters and default arguments are.
# Understanding the NumPy .clip() Method import numpy as np arr = np.array() arr.clip(a, a_min, a_max, out=None)
We can see that the method has four parameters, 2 of which are required. The table below breaks down the parameters of the NumPy clip method:
|Parameter||Description||Default Argument||Accepted Values|
|The array containing elements to clip||N/A||array-like|
|The minimum value. May be ||array-like or None|
|The maximum value. May be ||array-like or None|
|The output array.||ndarray|
In the following section, you’ll learn how to limit NumPy array values with a lower bound.
Limiting NumPy Array Minimum Values
In this section, you’ll learn how to limit NumPy array values by setting a minimum value. This will clip allow other values and return the minimum bound for any value that is below that value. Let’s generate an array containing the values from 0 through 10 and clip any values below 3:
# Limiting NumPy Arrays with a Lower Bound import numpy as np arr = np.arange(11) clipped = np.clip(a=arr, a_min=3, a_max=None) print(clipped) # Returns: [ 3 3 3 3 4 5 6 7 8 9 10]
Let’s break down what we did in the code above:
- We imported NumPy using the alias
- We created an array using the
arange()function, covering the values from 0 through 10
- We created a new array, clipping any values below 3 to be represented by 3
The NumPy clip() function requires that either the
a_min or the
a_max argument is not
None. In the following section, you’ll learn how to limit an array based on maximum values.
Limiting NumPy Array Maximum Values
Similarly, you can clip a NumPy array by setting a value for the
a_max= parameter. When this parameter is set, you can set the
a_min= parameter to
None, meaning that there will be no lower bound. Let’s use the same array containing the values from 0 through 10 and clip any values above 7.
# Limiting NumPy Arrays with an Upper Bound import numpy as np arr = np.arange(11) clipped = np.clip(a=arr, a_min=None, a_max=7) print(clipped) # Returns: [0 1 2 3 4 5 6 7 7 7 7]
In the code block above, we passed in a clip maximum value of 7. This means that any value greater than 7 is replaced with 7.
Limiting NumPy Array Minimum and Maximum Values
clip() function allows you to specify both lower and upper clipping values, thereby allowing you to remove extreme values from an array. This can be done by passing in values into both the
Let’s see how we can replace values smaller than 3 and larger than 7 with their respective bounds.
# Limiting NumPy Arrays with Upper and Lower Bounds import numpy as np arr = np.arange(11) clipped = np.clip(a=arr, a_min=3, a_max=7) print(clipped) # Returns: [3 3 3 3 4 5 6 7 7 7 7]
In the code block above, we passed in both a lower and an upper bound to clip values with. This resulted in values on either end of the array being clipped.
Clipping NumPy Arrays In Place
In this final section, you’ll learn how to clip NumPy arrays in place. In the previous sections, we clipped arrays by assigning them to a new variable. However, we can also use the
out= parameter to specify to which array our clipped array should be assigned.
In order to clip a NumPy array in place, you can simply specify the
out= parameter to be the array itself. This means that you do not need to re-assign the array to another variable.
# Clipping Arrays In Place Using NumPy clip() import numpy as np arr = np.arange(11) np.clip(a=arr, a_min=3, a_max=7, out=arr) print(arr) # Returns: [3 3 3 3 4 5 6 7 7 7 7]
In the code block above, we passed the array
arr to the
out= parameter. Note also, that we did not assign the result to another variable directly. This allows us to clip the NumPy array in place.
In this tutorial, you learned how to use the NumPy clip method to limit values in a NumPy array by minimum and maximum ranges. You first learned how to understand the different parameters and default arguments of the
np.clip() function. Then, you learned how to set the lower and upper limits of the array. Finally, you learned how to clip a NumPy array in place.
To learn more about related topics, check out the tutorials below: