NumPy ndarray.clip()
The numpy.ndarray.clip()
method is used to limit the values in an array within a specified range.
It sets values below the minimum limit to the minimum and values above the maximum limit to the maximum.
Syntax
ndarray.clip(min=None, max=None, out=None, **kwargs)
Parameters
Parameter | Type | Description |
---|---|---|
min | scalar or array-like, optional | The minimum value to clip elements to. If None , no lower clipping is performed. |
max | scalar or array-like, optional | The maximum value to clip elements to. If None , no upper clipping is performed. |
out | ndarray, optional | Alternative output array for storing the result. Must have the same shape as the input array. |
**kwargs | dict, optional | Additional arguments passed to methods. |
Return Value
Returns an array where all values are clipped to the specified range. If an output array is provided, the result is stored in it.
Examples
1. Clipping Values in a NumPy Array
In this example, we create a NumPy array and apply the clip()
method to restrict its values within a given range.
import numpy as np
# Create an array with values ranging from -10 to 10
arr = np.array([-10, -5, 0, 5, 10])
# Clip values between -3 and 7
clipped_arr = arr.clip(min=-3, max=7)
# Print the original and clipped arrays
print("Original Array:", arr)
print("Clipped Array:", clipped_arr)
Output:
Original Array: [-10 -5 0 5 10]
Clipped Array: [-3 -3 0 5 7]
Values less than -3
are replaced with -3
, and values greater than 7
are replaced with 7
.
2. Using an Array as Minimum and Maximum Values
We can also specify different min and max values for each element by providing an array.
import numpy as np
# Define an input array
arr = np.array([2, 8, 15, 3, 10])
# Define element-wise min and max values
min_vals = np.array([1, 5, 10, 2, 7])
max_vals = np.array([5, 10, 18, 4, 12])
# Apply element-wise clipping
clipped_arr = arr.clip(min=min_vals, max=max_vals)
# Print the result
print("Original Array:", arr)
print("Clipped Array:", clipped_arr)
Output:
Original Array: [ 2 8 15 3 10]
Clipped Array: [ 2 8 15 3 10]
Each element is clipped individually according to the corresponding value in min_vals
and max_vals
.
3. Storing the Clipped Output in an Existing Array
We can use the out
parameter to store the clipped result in an existing array instead of creating a new one.
import numpy as np
# Define an input array
arr = np.array([1, 7, 12, 20, -4])
# Define a preallocated output array
out_arr = np.empty_like(arr)
# Clip values between 0 and 10 and store in out_arr
arr.clip(min=0, max=10, out=out_arr)
# Print the output array
print("Clipped Output Array:", out_arr)
Output:
Clipped Output Array: [ 1 7 10 10 0]
The clipped result is stored in out_arr
instead of creating a new array.
4. Clipping with Only a Minimum or Maximum Value
If we specify only the min
parameter, all values below it are replaced, and if we specify only the max
parameter, all values above it are replaced.
import numpy as np
# Define an input array
arr = np.array([3, -2, 5, 8, -10, 15])
# Clip only the minimum value (all values below 0 are set to 0)
clipped_min = arr.clip(min=0)
# Clip only the maximum value (all values above 10 are set to 10)
clipped_max = arr.clip(max=10)
# Print results
print("Clipping Min (>=0):", clipped_min)
print("Clipping Max (<=10):", clipped_max)
Output:
Clipping Min (>=0): [ 3 0 5 8 0 15]
Clipping Max (<=10): [ 3 -2 5 8 -10 10]
In the first case, negative values are replaced with 0
, while in the second case, values greater than 10
are replaced with 10
.