numpy.digitize

numpy.digitize(x, bins)

Return the indices of the bins to which each value in input array belongs.

Each index i returned is such that bins[i-1] <= x < bins[i] if bins is monotonically increasing, or bins[i-1] > x >= bins[i] if bins is monotonically decreasing. If values in x are beyond the bounds of bins, 0 or len(bins) is returned as appropriate.

Parameters:

x : array_like

Input array to be binned. It has to be 1-dimensional.

bins : array_like

Array of bins. It has to be 1-dimensional and monotonic.

Returns:

out : ndarray of ints

Output array of indices, of same shape as x.

Raises:

ValueError :

If the input is not 1-dimensional, or if bins is not monotonic.

TypeError :

If the type of the input is complex.

Notes

If values in x are such that they fall outside the bin range, attempting to index bins with the indices that digitize returns will result in an IndexError.

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

Previous topic

numpy.bincount

Next topic

Mathematical functions

This Page