[Numpy-discussion] Inverting argsort(a, axis=0) to obtain column-wise ranks

John Schulman joschu at caltech.edu
Mon Sep 20 01:19:13 EDT 2010


D'oh, Zachary already gave that answer.

On Sun, Sep 19, 2010 at 10:17 PM, John Schulman <joschu at caltech.edu> wrote:
> Argsort twice and you get the rank.
>
> a1.argsort(axis=0).argsort(axis=0)
>
> That's because argsort is it's own inverse when applied to the ranks.
>
> On Tue, Sep 7, 2010 at 1:01 PM, Alexander Michael <lxander.m at gmail.com> wrote:
>> Calculating ranks by inverting the results of an argsort is
>> straightforward and fast for 1D arrays:
>>
>> indices = argsort(a1)
>> ranks = zeros_like(indices)
>> ranks[indices] = arange(len(indices))
>>
>> I was wondering if there was an equally pithy way to do this for
>> multiple data samples stored column-wise in a 2D array. That is, is
>> there a trick to invert the results of argsort(a2, axis=0) without
>> iterating (in python) over the columns?
>>
>> Thanks,
>> Alex
>> _______________________________________________
>> NumPy-Discussion mailing list
>> NumPy-Discussion at scipy.org
>> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>>
>>
>



More information about the NumPy-Discussion mailing list