tf.searchsorted

Searches for where a value would go in a sorted sequence.

This is not a method for checking containment (like python in).

The typical use case for this operation is "binning", "bucketing", or "discretizing". The values are assigned to bucket-indices based on the edges listed in sorted_sequence. This operation returns the bucket-index for each value.

edges = [-1, 3.3, 9.1, 10.0]
values = [0.0, 4.1, 12.0]
tf.searchsorted(edges, values).numpy()
array([1, 2, 4], dtype=int32)

The side argument controls which index is returned if a value lands exactly on an edge:

seq = [0, 3, 9, 10, 10]
values = [0, 4, 10]
tf.searchsorted(seq, values).numpy()
array([0, 2, 3], dtype=int32)
tf.searchsorted(seq, values, side="right").numpy()
array([1, 2, 5], dtype=int32)

The axis is not settable for this operation. It always operates on the innermost dimension (