NumPy searchsorted finds insertion positions without changing the source array

A sorted sequence can answer where a new value belongs before any insertion is performed. NumPy searchsorted returns positions that preserve ascending order. Academia Master Trader presents this English lesson for readers in Syria, using constructed arrays to explain insertion boundaries, repeated values and the meaning of positions returned with a sorter.

The result is a position rather than an updated array

After import numpy as np, define a = np.array([3,7,7,12]). The call np.searchsorted(a,9) returns 3. Inserting nine before position three would place it between the final seven and twelve.

Searchsorted does not insert nine. The original array remains [3,7,7,12]. Its result is an integer identifying an insertion point. Distinguish that position from the input value and from any later array that a separate insertion operation might create.

NumPy insertion boundaries

Left and right choose opposite boundaries of equal values

The call np.searchsorted(a,7,side='left') returns 1. The left boundary comes before both existing sevens. This is also the default side, so np.searchsorted(a,7) produces the same result.

The call np.searchsorted(a,7,side='right') returns 3. The right boundary comes after both sevens and before twelve. Both positions preserve sorted order. The side argument determines how the new value is placed relative to entries equal to it.

For a value absent from the array, left and right can agree. Both sides return three for nine in this example because there is no group of equal nines separating the boundaries. Write the repeated values beside each boundary when explaining why the answers differ.

Endpoint positions are valid insertion locations

The call np.searchsorted(a,[1,7,15]) returns [0,1,4]. One belongs before the first entry. Seven uses the default left boundary. Fifteen belongs after the final entry.

Position four is a valid insertion point for an array of length four, but it is not an existing element index. Reading a[4] would be outside the array. A boundary result must not automatically be used to retrieve an existing value without checking whether it lies within the element range.

When the query values form an array, the returned positions have that query array's shape. Each query is located against the same source sequence; the queries are not inserted one after another while the source grows.

For example, np.searchsorted(a,[[1,9],[7,15]]) returns [[0,3],[1,4]]. This two by two result keeps the arrangement of the query values. The source remains a one dimensional sequence even when the query contains several dimensions.

A sorter refers to a sorted view of the positions

Without a sorter, the source must already be sorted in ascending order. For b = np.array([12,3,7,7]), define order = np.argsort(b). The call np.searchsorted(b,9,sorter=order) returns 3.

That result belongs to the sorted sequence b[order], which is [3,7,7,12], not directly to the original arrangement of b. Supplying a sorter does not rearrange b. Keep the original positions and sorted positions distinct throughout the explanation.

Continue learning with Academia Master Trader at academiamastertrader.com.br and in Aprenda. Record the sorted source, query value and side before interpreting an insertion boundary.