What is the most efficient way of finding the ranks of one numpy array in another?

Viewed 50

Say I have two arrays:

a = np.array([2, 5, 4, 3, 1])
b = np.array([4.5, 1.5, 3.5])

I want to find the rank of each element of a if it were in b. So this would be the desired output:

[1, 3, 2, 1, 0]

The following code technically works for small arrays but is extremely slow if a and b are 10,000+ in size:

ranks = [rankdata(b + [i])[-1] - 1 for i in a]

What is the most efficient way of achieving this result?

1 Answers

Use searchsorted

ind = np.argsort(y)
np.searchsorted(y, x, sorter=ind)
# array([1, 3, 2, 1, 0], dtype=int64)

If y has duplicates, you may have to tinker with the side argument.

Related