ML Foundations

Map searchsorted positions back to an unsorted NumPy array

Translate sorted insertion positions to original rows and verify exact matches, including the endpoint.

Example results for Map searchsorted positions back to an unsorted NumPy array. 50: sorted position=4, no element at or above query
Illustration: Original illustration by Tensor Almanac
Evidence and testing

Review record: Tensor Almanac automated evidence gate (no independent human review). Example versions, recorded outputs and limitations appear below.

An insertion position is in sorted order

Suppose an array stores scores in an order that must stay aligned with another table. NumPy searchsorted can search the conceptual sorted order using an argsort permutation. Its result is a position in that sorted order, not the position of a row in the original array. [1, 2]

The default left side gives the position before equal values. It can return the length of the array when the query is larger than every entry. Treating that endpoint as an ordinary element index will fail. [1]

References: [1], [2]

Translate only an in-range position

Run python examples/numpy_searchsorted_sorter_positions.py. The sorter contains original indices in ascending value order. After finding a position, check that it is below the array length, then use sorter[position] to obtain the original index. Compare the original value with the query if an exact match is required. [1, 2]

For 20, the translated index is 2 and the value matches. For 25, the next value is 30 at index 3, but the equality check is false. For 50, the insertion position equals the array length, so the example reports that there is no candidate instead of indexing out of bounds. [1]

Executed with Python 3.12.14; output and checksum saved in private evidence.

import numpy as np

values = np.array([40, 10, 20, 30])
order = np.argsort(values)
queries = [20, 25, 50]
for query in queries:
    position = int(np.searchsorted(values, query, sorter=order))
    if position < len(order):
        original_index = int(order[position])
        match = bool(values[original_index] == query)
        print(f'{query}: sorted position={position}, original index={original_index}, value={values[original_index]}, exact match={match}')
    else:
        print(f'{query}: sorted position={position}, no element at or above query')

Recorded output

20: sorted position=1, original index=2, value=20, exact match=True
25: sorted position=2, original index=3, value=30, exact match=False
50: sorted position=4, no element at or above query

References: [1], [2]

Define tie and missing-value rules

With duplicate values, side="left" gives the first insertion position among equals in the sorted view. If a particular original row among equal values matters, define and check a separate tie policy; an insertion position alone does not supply that policy. [1, 2]

The example uses a one-dimensional finite integer array and ascending order. It does not address missing values, descending order, multidimensional indexing, or runtime comparisons. Keep the sorter consistent with the array whenever values change. [1]

References: [1], [2]

Sources

  1. NumPy searchsorted
  2. NumPy argsort