pellungrobe
pellungrobe

Reputation: 93

Numpy searchsorted descending order

I am trying to understand how to correctly use numpy.searchsorted to find the correct index where to insert an element. When i run the following:

e = np.array([9,8,7,6])
np.searchsorted(e,5)

The output I get is 0, which is wrong as I would expect 4 as the answer. Is searchsorted usable for arrays ordered in descending order? What options do I have if I need to keep the array in descending order? Thanks a lot!

Upvotes: 9

Views: 5205

Answers (3)

akuiper
akuiper

Reputation: 214927

You can search the reversed array, and then reverse the index later:

e = np.array([9,8,7,6])
e.size - np.searchsorted(e[::-1], 5, side = "right")
# 4

Notice the side parameter here needs to be opposite to directly search the original array if you want the result to be consistent.

Upvotes: 16

John Zwinck
John Zwinck

Reputation: 249123

You can do it using the sorter optional argument. As per the documentation this is usually the result of np.argsort(e):

array([3, 2, 1, 0])

Which you can produce more efficiently this way:

np.arange(len(e)-1, -1, -1)

Then:

np.searchsorted(e, 7.5, sorter=np.arange(len(e)-1, -1, -1))

Gives you 2.

Upvotes: 2

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476544

As is described in the documentation:

numpy.searchsorted(a, v, side='left', sorter=None)

Find indices where elements should be inserted to maintain order.

Find the indices into a sorted array a such that, if the corresponding elements in v were inserted before the indices, the order of a would be preserved.

Parameters:

  • a : 1-D array_like

    Input array. If sorter is None, then it must be sorted in ascending order, otherwise sorter must be an array of indices that sort it.

(...)

(formatting added)

So you have to provide an array sorted in ascending order.

Nevertheless it is easy to solve this issue for numerical data: you simply use the negation:

e = np.array([9,8,7,6])
np.searchsorted(-e,-5)
#               ^  ^

You can also use the reversed array (for instance for non-numerical data) as is proposed by @Psidom.

Upvotes: 21

Related Questions