![](/img/trans.png)
[英]Most efficient way to test whether each element from one array exists in a another array, using numpy
[英]What is the most efficient way of finding the ranks of one numpy array in another?
假設我有兩個數組:
a = np.array([2, 5, 4, 3, 1])
b = np.array([4.5, 1.5, 3.5])
如果它在b
中,我想找到a
的每個元素的等級。 所以這將是所需的輸出:
[1, 3, 2, 1, 0]
以下代碼在技術上適用於小型數組,但如果a
和b
的大小為 10,000+,則速度非常慢:
ranks = [rankdata(b + [i])[-1] - 1 for i in a]
實現這一結果的最有效方法是什么?
ind = np.argsort(y)
np.searchsorted(y, x, sorter=ind)
# array([1, 3, 2, 1, 0], dtype=int64)
如果y
有重復項,您可能需要修改side
參數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.