[SciPy-user] Element ranks for an array

Robert Kern robert.kern@gmail....
Tue Nov 6 18:30:24 CST 2007


Alok Singhal wrote:
> Hi,
> 
> On 06/11/07: 18:49, R. Padraic Springuel wrote:
>> Does scipy have a function that can provide the element ranks by size 
>> for a 1-d array?
>>
>> I.e. a function that would return [3,4,2,1] or [2,1,3,4] (depending on 
>> ascending or descending ranking) for array([50, 10, 62, 1000]).
> 
> I assume you meant [4, 3, 1, 2] in the first case.
> 
> Look at argsort() in numpy.
> 
> This should do what you want:
> 
> def rank(x, ascending=True):
> 	i = numpy.argsort(x) + 1
> 	if ascending:
> 		return i
> 	else:
> 		return i[::-1]

No, this is incorrect. He correctly wanted [3,4,2,1] for the descending ranking.

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco


More information about the SciPy-user mailing list