numpy.digitize
-
numpy.digitize(x, bins, right=False) -
Return the indices of the bins to which each value in input array belongs.
Each index
ireturned is such thatbins[i-1] <= x < bins[i]ifbinsis monotonically increasing, orbins[i-1] > x >= bins[i]ifbinsis monotonically decreasing. If values inxare beyond the bounds ofbins, 0 orlen(bins)is returned as appropriate. If right is True, then the right bin is closed so that the indexiis such thatbins[i-1] < x <= bins[i]or bins[i-1] >= x > bins[i]`` ifbinsis monotonically increasing or decreasing, respectively.Parameters: x : array_like
Input array to be binned. Prior to Numpy 1.10.0, this array had to be 1-dimensional, but can now have any shape.
bins : array_like
Array of bins. It has to be 1-dimensional and monotonic.
right : bool, optional
Indicating whether the intervals include the right or the left bin edge. Default behavior is (right==False) indicating that the interval does not include the right edge. The left bin end is open in this case, i.e., bins[i-1] <= x < bins[i] is the default behavior for monotonically increasing bins.
Returns: out : ndarray of ints
Output array of indices, of same shape as
x.Raises: ValueError
If
binsis not monotonic.TypeError
If the type of the input is complex.
Notes
If values in
xare such that they fall outside the bin range, attempting to indexbinswith the indices thatdigitizereturns will result in an IndexError.New in version 1.10.0.
np.digitizeis implemented in terms ofnp.searchsorted. This means that a binary search is used to bin the values, which scales much better for larger number of bins than the previous linear search. It also removes the requirement for the input array to be 1-dimensional.Examples
>>> x = np.array([0.2, 6.4, 3.0, 1.6]) >>> bins = np.array([0.0, 1.0, 2.5, 4.0, 10.0]) >>> inds = np.digitize(x, bins) >>> inds array([1, 4, 3, 2]) >>> for n in range(x.size): ... print bins[inds[n]-1], "<=", x[n], "<", bins[inds[n]] ... 0.0 <= 0.2 < 1.0 4.0 <= 6.4 < 10.0 2.5 <= 3.0 < 4.0 1.0 <= 1.6 < 2.5
>>> x = np.array([1.2, 10.0, 12.4, 15.5, 20.]) >>> bins = np.array([0, 5, 10, 15, 20]) >>> np.digitize(x,bins,right=True) array([1, 2, 3, 4, 4]) >>> np.digitize(x,bins,right=False) array([1, 3, 3, 4, 5])
© 2008–2016 NumPy Developers
Licensed under the NumPy License.
https://docs.scipy.org/doc/numpy-1.10.1/reference/generated/numpy.digitize.html