简体   繁体   English

使用 Python/NumPy 对数组中的项目进行排名,无需对数组进行两次排序

[英]Rank items in an array using Python/NumPy, without sorting array twice

I have an array of numbers and I'd like to create another array that represents the rank of each item in the first array.我有一个数字数组,我想创建另一个数组来表示第一个数组中每个项目的排名。 I'm using Python and NumPy.我正在使用 Python 和 NumPy。

For example:例如:

array = [4,2,7,1]
ranks = [2,1,3,0]

Here's the best method I've come up with:这是我想出的最好的方法:

array = numpy.array([4,2,7,1])
temp = array.argsort()
ranks = numpy.arange(len(array))[temp.argsort()]

Are there any better/faster methods that avoid sorting the array twice?有没有更好/更快的方法可以避免对数组进行两次排序?

Use argsort twice, first to obtain the order of the array, then to obtain ranking:使用 argsort 两次,首先获取数组的顺序,然后获取排名:

array = numpy.array([4,2,7,1])
order = array.argsort()
ranks = order.argsort()

When dealing with 2D (or higher dimensional) arrays, be sure to pass an axis argument to argsort to order over the correct axis.处理 2D(或更高维)数组时,请确保将轴参数传递给 argsort 以在正确的轴上排序。

This question is a few years old, and the accepted answer is great, but I think the following is still worth mentioning.这个问题已经有几年了,接受的答案很好,但我认为以下仍然值得一提。 If you don't mind the dependency on scipy , you can use scipy.stats.rankdata :如果你不介意对scipy的依赖,你可以使用scipy.stats.rankdata

In [22]: from scipy.stats import rankdata

In [23]: a = [4, 2, 7, 1]

In [24]: rankdata(a)
Out[24]: array([ 3.,  2.,  4.,  1.])

In [25]: (rankdata(a) - 1).astype(int)
Out[25]: array([2, 1, 3, 0])

A nice feature of rankdata is that the method argument provides several options for handling ties. rankdata一个很好的特性是method参数提供了几个处理关系的选项。 For example, there are three occurrences of 20 and two occurrences of 40 in b :例如,在b有 3 次 20 和 2 次 40 :

In [26]: b = [40, 20, 70, 10, 20, 50, 30, 40, 20]

The default assigns the average rank to the tied values:默认将平均排名分配给绑定值:

In [27]: rankdata(b)
Out[27]: array([ 6.5,  3. ,  9. ,  1. ,  3. ,  8. ,  5. ,  6.5,  3. ])

method='ordinal' assigns consecutive ranks: method='ordinal'分配连续的等级:

In [28]: rankdata(b, method='ordinal')
Out[28]: array([6, 2, 9, 1, 3, 8, 5, 7, 4])

method='min' assigns the minimum rank of the tied values to all the tied values: method='min'将绑定值的最小等级分配给所有绑定值:

In [29]: rankdata(b, method='min')
Out[29]: array([6, 2, 9, 1, 2, 8, 5, 6, 2])

See the docstring for more options.有关更多选项,请参阅文档字符串。

Use advanced indexing on the left-hand side in the last step:在最后一步的左侧使用高级索引

array = numpy.array([4,2,7,1])
temp = array.argsort()
ranks = numpy.empty_like(temp)
ranks[temp] = numpy.arange(len(array))

This avoids sorting twice by inverting the permutation in the last step.这通过反转最后一步中的排列来避免排序两次。

For a vectorized version of an averaged rank, see below.有关平均排名的矢量化版本,请参见下文。 I love np.unique, it really widens the scope of what code can and cannot be efficiently vectorized.我喜欢 np.unique,它确实拓宽了可以和不可以有效矢量化代码的范围。 Aside from avoiding python for-loops, this approach also avoids the implicit double loop over 'a'.除了避免python for循环之外,这种方法还避免了'a'上的隐式双循环。

import numpy as np

a = np.array( [4,1,6,8,4,1,6])

a = np.array([4,2,7,2,1])
rank = a.argsort().argsort()

unique, inverse = np.unique(a, return_inverse = True)

unique_rank_sum = np.zeros_like(unique)
np.add.at(unique_rank_sum, inverse, rank)
unique_count = np.zeros_like(unique)
np.add.at(unique_count, inverse, 1)

unique_rank_mean = unique_rank_sum.astype(np.float) / unique_count

rank_mean = unique_rank_mean[inverse]

print rank_mean

I tried to extend both solution for arrays A of more than one dimension, supposing you process your array row-by-row (axis=1).我尝试为多维数组 A 扩展这两种解决方案,假设您逐行处理数组(轴 = 1)。

I extended the first code with a loop on rows;我用行上的循环扩展了第一个代码; probably it can be improved可能可以改进

temp = A.argsort(axis=1)
rank = np.empty_like(temp)
rangeA = np.arange(temp.shape[1])
for iRow in xrange(temp.shape[0]): 
    rank[iRow, temp[iRow,:]] = rangeA

And the second one, following k.rooijers suggestion, becomes:第二个,按照 k.rooijers 的建议,变成:

temp = A.argsort(axis=1)
rank = temp.argsort(axis=1)

I randomly generated 400 arrays with shape (1000,100);我随机生成了 400 个形状为 (1000,100) 的数组; the first code took about 7.5, the second one 3.8.第一个代码花费了大约 7.5,第二个代码花费了 3.8。

Apart from the elegance and shortness of solutions, there is also the question of performance.除了解决方案的优雅和简短之外,还有性能问题。 Here is a little benchmark:这是一个小基准:

import numpy as np
from scipy.stats import rankdata
l = list(reversed(range(1000)))

%%timeit -n10000 -r5
x = (rankdata(l) - 1).astype(int)
>>> 128 µs ± 2.72 µs per loop (mean ± std. dev. of 5 runs, 10000 loops each)

%%timeit -n10000 -r5
a = np.array(l)
r = a.argsort().argsort()
>>> 69.1 µs ± 464 ns per loop (mean ± std. dev. of 5 runs, 10000 loops each)

%%timeit -n10000 -r5
a = np.array(l)
temp = a.argsort()
r = np.empty_like(temp)
r[temp] = np.arange(len(a))
>>> 63.7 µs ± 1.27 µs per loop (mean ± std. dev. of 5 runs, 10000 loops each)

Use argsort() twice will do it:使用argsort()两次即可:

>>> array = [4,2,7,1]
>>> ranks = numpy.array(array).argsort().argsort()
>>> ranks
array([2, 1, 3, 0])

I tried the above methods, but failed because I had many zeores.我尝试了上述方法,但失败了,因为我有很多零。 Yes, even with floats duplicate items may be important.是的,即使有浮动,重复的项目也可能很重要。

So I wrote a modified 1D solution by adding a tie-checking step:所以我通过添加一个平局检查步骤编写了一个修改后的一维解决方案:

def ranks (v):
    import numpy as np
    t = np.argsort(v)
    r = np.empty(len(v),int)
    r[t] = np.arange(len(v))
    for i in xrange(1, len(r)):
        if v[t[i]] <= v[t[i-1]]: r[t[i]] = r[t[i-1]]
    return r

# test it
print sorted(zip(ranks(v), v))

I believe it's as efficient as it can be.我相信它尽可能高效。

I liked the method by k.rooijers, but as rcoup wrote, repeated numbers are ranked according to array position.我喜欢 k.rooijers 的方法,但正如 rcoup 所写,重复的数字根据数组位置排列。 This was no good for me, so I modified the version to postprocess the ranks and merge any repeated numbers into a combined average rank:这对我没有好处,所以我修改了版本以对排名进行后处理并将任何重复的数字合并为一个组合的平均排名:

import numpy as np
a = np.array([4,2,7,2,1])
r = np.array(a.argsort().argsort(), dtype=float)
f = a==a
for i in xrange(len(a)):
   if not f[i]: continue
   s = a == a[i]
   ls = np.sum(s)
   if ls > 1:
      tr = np.sum(r[s])
      r[s] = float(tr)/ls
   f[s] = False

print r  # array([ 3. ,  1.5,  4. ,  1.5,  0. ])

I hope this might help others too, I tried to find anothers solution to this, but couldn't find any...我希望这也可以帮助其他人,我试图找到其他人的解决方案,但找不到任何......

argsort and slice are symmetry operations. argsort 和 slice 是对称操作。

try slice twice instead of argsort twice.尝试切片两次而不是 argsort 两次。 since slice is faster than argsort因为 slice 比 argsort 快

array = numpy.array([4,2,7,1])
order = array.argsort()
ranks = np.arange(array.shape[0])[order][order]

More general version of one of the answers:答案之一的更一般版本:

In [140]: x = np.random.randn(10, 3)

In [141]: i = np.argsort(x, axis=0)

In [142]: ranks = np.empty_like(i)

In [143]: np.put_along_axis(ranks, i, np.repeat(np.arange(x.shape[0])[:,None], x.shape[1], axis=1), axis=0)

See How to use numpy.argsort() as indices in more than 2 dimensions?请参阅如何使用 numpy.argsort() 作为超过 2 个维度的索引? to generalize to more dims.推广到更多的暗淡。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM