简体   繁体   English

在另一个数组中找到一个 numpy 数组的行列的最有效方法是什么?

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

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 .如果它在b中,我想找到a的每个元素的等级。 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:以下代码在技术上适用于小型数组,但如果ab的大小为 10,000+,则速度非常慢:

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

What is the most efficient way of achieving this result?实现这一结果的最有效方法是什么?

Use searchsorted使用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.如果y有重复项,您可能需要修改side参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用numpy测试一个数组中的每个元素是否存在于另一个数组中的最有效方法 - Most efficient way to test whether each element from one array exists in a another array, using numpy 检查 NumPy 数组中是否存在值的最有效方法是什么? - What is the most efficient way to check if a value exists in a NumPy array? 访问存储在 NumPy 数组中的树节点的最有效方法是什么 - What is most efficient way to access nodes of a tree stored in a NumPy array 在Numpy数组中匹配模板的最有效方法是什么? - What is the most efficient way to match templates in a Numpy array? 查找一个数组中哪些元素与另一个元素中的任何元素接近的最有效方法是什么? - What's the most efficient way to find which elements of one array are close to any element in another? 索引 Numpy 矩阵的最有效方法是什么? - What is the most efficient way of indexing Numpy matrices? 基于另一个数组提取部分数组的最有效方法 - Most efficient way to extract parts of one array based on another 将numpy数组转换为字符串的最有效方法 - Most efficient way to convert numpy array to string 在 numpy 数组中查找模式的最有效方法 - Most efficient way to find mode in numpy array 反转 numpy 阵列的最有效方法 - Most efficient way to reverse a numpy array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM