[Numpy-discussion] numpy function to compute sample ranks

Wes McKinney wesmckinn at gmail.com
Sun Nov 2 14:37:30 EST 2008


Try rankdata in scipy.stats

On Nov 2, 2008, at 1:35 PM, Keith Goodman wrote:

> On Sun, Nov 2, 2008 at 10:24 AM, Abhimanyu Lad  
> <abhimanyulad at gmail.com> wrote:
>> Is there a direct or indirect way in numpy to compute the sample  
>> ranks of a
>> given array, i.e. the equivalent of rank() in R.
>>
>> I am looking for:
>> rank(array([6,8,4,1,9])) -> array([2,3,1,0,4])
>>
>> Is there some clever use of argsort() that I am missing?
>
> If there are no NaNs and no ties, then:
>
>>> x = np.array([6,8,4,1,9])
>>> x.argsort().argsort()
>    array([2, 3, 1, 0, 4])
>
> If you have ties, then scipy has a ranking function.
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion at scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion




More information about the NumPy-Discussion mailing list