Skip to content

NumPy clip(): Limit Array Values with Min and Max

NumPy Clip Limit Array Values with Min and Max Cover Image

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 .clip() function works
  • 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

What does the NumPy clip() function do?

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:

ParameterDescriptionDefault ArgumentAccepted Values
a=The array containing elements to clipN/Aarray-like
a_min=The minimum value. May be None if a_max is not NoneNonearray-like or None
a_max=The maximum value. May be None if a_min is not NoneNonearray-like or None
out=The output array.Nonendarray
The parameters and the default arguments of the NumPy clip() method

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:

  1. We imported NumPy using the alias np
  2. We created an array using the arange() function, covering the values from 0 through 10
  3. 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

The NumPy 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 a_min and a_max parameters.

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.

Conclusion

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.

Additional Resources

To learn more about related topics, check out the tutorials below:

Nik Piepenbreier

Nik is the author of datagy.io and has over a decade of experience working with data analytics, data science, and Python. He specializes in teaching developers how to use Python for data science using hands-on tutorials.View Author posts

Leave a Reply

Your email address will not be published. Required fields are marked *