MCPcopy Create free account
hub / github.com/numpy/numpy / digitize

Function digitize

numpy/lib/function_base.py:5626–5733  ·  view source on GitHub ↗

Return the indices of the bins to which each value in input array belongs. ========= ============= ============================ `right` order of bins returned index `i` satisfies ========= ============= ============================ ``False`` increasing ``bins[i-1]

(x, bins, right=False)

Source from the content-addressed store, hash-verified

5624
5625@array_function_dispatch(_digitize_dispatcher)
5626def digitize(x, bins, right=False):
5627 """
5628 Return the indices of the bins to which each value in input array belongs.
5629
5630 ========= ============= ============================
5631 `right` order of bins returned index `i` satisfies
5632 ========= ============= ============================
5633 ``False`` increasing ``bins[i-1] <= x < bins[i]``
5634 ``True`` increasing ``bins[i-1] < x <= bins[i]``
5635 ``False`` decreasing ``bins[i-1] > x >= bins[i]``
5636 ``True`` decreasing ``bins[i-1] >= x > bins[i]``
5637 ========= ============= ============================
5638
5639 If values in `x` are beyond the bounds of `bins`, 0 or ``len(bins)`` is
5640 returned as appropriate.
5641
5642 Parameters
5643 ----------
5644 x : array_like
5645 Input array to be binned. Prior to NumPy 1.10.0, this array had to
5646 be 1-dimensional, but can now have any shape.
5647 bins : array_like
5648 Array of bins. It has to be 1-dimensional and monotonic.
5649 right : bool, optional
5650 Indicating whether the intervals include the right or the left bin
5651 edge. Default behavior is (right==False) indicating that the interval
5652 does not include the right edge. The left bin end is open in this
5653 case, i.e., bins[i-1] <= x < bins[i] is the default behavior for
5654 monotonically increasing bins.
5655
5656 Returns
5657 -------
5658 indices : ndarray of ints
5659 Output array of indices, of same shape as `x`.
5660
5661 Raises
5662 ------
5663 ValueError
5664 If `bins` is not monotonic.
5665 TypeError
5666 If the type of the input is complex.
5667
5668 See Also
5669 --------
5670 bincount, histogram, unique, searchsorted
5671
5672 Notes
5673 -----
5674 If values in `x` are such that they fall outside the bin range,
5675 attempting to index `bins` with the indices that `digitize` returns
5676 will result in an IndexError.
5677
5678 .. versionadded:: 1.10.0
5679
5680 `np.digitize` is implemented in terms of `np.searchsorted`. This means
5681 that a binary search is used to bin the values, which scales much better
5682 for larger number of bins than the previous linear search. It also removes
5683 the requirement for the input array to be 1-dimensional.

Callers 9

test_forwardMethod · 0.90
test_reverseMethod · 0.90
test_randomMethod · 0.90
test_right_basicMethod · 0.90
test_right_openMethod · 0.90
test_monotonicMethod · 0.90
test_return_typeMethod · 0.90

Calls

no outgoing calls

Tested by 9

test_forwardMethod · 0.72
test_reverseMethod · 0.72
test_randomMethod · 0.72
test_right_basicMethod · 0.72
test_right_openMethod · 0.72
test_monotonicMethod · 0.72
test_return_typeMethod · 0.72