mindspore.ops.bucketize

View Source On Gitee
mindspore.ops.bucketize(input, boundaries, *, right=False)[source]

Bucketizes input based on boundaries. If right is False, the left boundary is closed. For each element x in input, the returned index satisfies the following rules:

\[\begin{split}\begin{cases} boundaries[i-1] < x <= boundaries[i], & \text{if right} = False\\ boundaries[i-1] <= x < boundaries[i], & \text{if right} = True \end{cases}\end{split}\]
Parameters
  • input (Tensor) – A tensor containing the search value(s).

  • boundaries (list) – A sorted list of boundary values of the buckets.

Keyword Arguments

right (bool, optional) – if False, gets the lower bound index for each value in input from boundaries; If True, gets the upper bound index instead. Default: False.

Returns

Tensor, the indexes Tensor, with the same shape as the input, and data type is int32.

Raises
Supported Platforms:

Ascend GPU CPU

Examples

>>> import numpy as np
>>> from mindspore import Tensor, ops
>>> input = Tensor(np.array([[3, 6, 9], [3, 6, 9]]))
>>> boundaries = list(np.array([1., 3., 5., 7., 9.]))
>>> output = ops.bucketize(input, boundaries, right=True)
>>> print(output)
[[2 3 5]
 [2 3 5]]