numpy.clip()
methodThe numpy.clip()
function is used to clip a limit value in an input array.
The input array can be 1-dimensional, 2-dimensional, etc.
In this article, we’ll focus on using the numpy.clip()
method for a 2-dimensional array.
Note: A list of lists can be used to create a two-dimensional (2-D) array in Python.
numpy.clip(a, a_min, a_max, out=None)
a
: This is the input (2-D) array of values.a_min
: (optional) Indicates the minimum values to clip the array.a_max
: (optional) Indicates the maximum values to clip the array.out
: (optional) Specify the location where the result is stored.The numpy.clip()
method returns an array containing elements of a
. However, values less than the specified a_min
are replaced with a_min
, and values greater than the specified a_max
are replaced with a_max
.
The following code shows how to use the numpy.clip()
method for two-dimensional (2-D) arrays in Python.
# import numpyimport numpy as np# create 2D array using np.arraya = np.array([[7,3,4,8], [2,6,9,5]])# Create a min valuea_min = 3# Create a max valuea_max = 8# np.clip()result = np.clip(a, a_min, a_max)print(result)
numpy
library.a
.a_min
.a_max
.np.clip()
method to limit the input array a
to a minimum of 3
and a maximum of 8
.Note: Any value of array
a
smaller than thea_min
is replaced by thea_min
value and any value of arraya
greater than thea_max
is replaced by thea_max
value.
Free Resources