| View source on GitHub | 
Searches for where a value would go in a sorted sequence.
tf.searchsorted(
    sorted_sequence,
    values,
    side='left',
    out_type=tf.dtypes.int32,
    name=None
)
  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 (axis=-1). The operation will accept any number of outer dimensions. Here it is applied to the rows of a matrix:
sorted_sequence = [[0., 3., 8., 9., 10.],
                   [1., 2., 3., 4., 5.]]
values = [[9.8, 2.1, 4.3],
          [0.1, 6.6, 4.5, ]]
tf.searchsorted(sorted_sequence, values).numpy()
array([[4, 1, 2],
       [0, 5, 4]], dtype=int32)
 Note: This operation assumes thatsorted_sequenceis sorted along the innermost axis, maybe usingtf.sort(..., axis=-1). If the sequence is not sorted no error is raised and the content of the returned tensor is not well defined.
| Args | |
|---|---|
| sorted_sequence | N-D Tensorcontaining a sorted sequence. | 
| values | N-D Tensorcontaining the search values. | 
| side | 'left' or 'right'; 'left' corresponds to lower_bound and 'right' to upper_bound. | 
| out_type | The output type ( int32orint64). Default istf.int32. | 
| name | Optional name for the operation. | 
| Returns | |
|---|---|
| An N-D Tensorthe size ofvaluescontaining the result of applying either lower_bound or upper_bound (depending on side) to each value. The result is not a global index to the entireTensor, but the index in the last dimension. | 
| Raises | |
|---|---|
| ValueError | If the last dimension of sorted_sequence >= 2^31-1elements. If the total size ofvaluesexceeds2^31 - 1elements. If the firstN-1dimensions of the two tensors don't match. | 
    © 2022 The TensorFlow Authors. All rights reserved.
Licensed under the Creative Commons Attribution License 4.0.
Code samples licensed under the Apache 2.0 License.
    https://www.tensorflow.org/versions/r2.9/api_docs/python/tf/searchsorted