[SciPy-user] Element ranks for an array

Alok Singhal as8ca@virginia....
Tue Nov 6 18:20:37 CST 2007


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]

-Alok

-- 
Alok Singhal                               *   *          
Graduate Student, dept. of Astronomy   *           *     *
University of Virginia                                    
http://www.astro.virginia.edu/~as8ca/              *    * 


More information about the SciPy-user mailing list