Skip to content
fn

bucketize

→Tensor
bucketize(input: Tensor | Scalar, boundaries: Tensor, out_int32: _bool = ..., right: _bool = ...)

Implementing kernel

C++ engine symbols that back this Python API.

Return the bucket index of each element of input in boundaries.

For each v = values[i] returns the smallest j such that v < boundaries[j] (or v <= boundaries[j] when right=True). Equivalent to a per-element binary search.

Parameters

inputTensor | Scalar
Query values (arbitrary shape). A Python number is bucketed as a 0-d value on the boundaries' device. input and boundaries are promoted to their common dtype, as the reference does.
boundariesTensor
1-D, sorted, monotonically non-decreasing bucket edges.
rightbool= False
Use <= rather than < for the comparison.

Returns

Tensor

int64 tensor of bucket indices with the same shape as input.

Notes

Indices are in [0, len(boundaries)] — out-of-range queries map to the boundary slots. Sister function searchsorted has the same semantics but takes the sorted sequence as the first argument.

Examples

>>> import lucid
>>> bounds = lucid.tensor([1.0, 3.0, 5.0])
>>> lucid.bucketize(lucid.tensor([0.5, 2.0, 4.0, 6.0]), bounds)
Tensor([0, 1, 2, 3])