Issue
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?
Solution
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.
Answered By - d.b
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.