[SciPy-user] Element ranks for an array

Pierre GM pgmdevlist@gmail....
Tue Nov 6 18:10:41 CST 2007


On Tuesday 06 November 2007 18:49:56 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]).

For a 1D array
numpy.arange(1,len(x)+1)[x.argsort()]

or simply
scipy.stats.rankdata(x)

That'll give you ranking by descending order. Use the [::-1] to get the 
reverse.


More information about the SciPy-user mailing list