[SciPy-User] [SciPy-user] Ranking a list of numbers
Thu Jun 3 12:07:05 CDT 2010
Did you mean
import numpy as np
a = np.array([ 0.012, 0.08, 2, 0.5, 0.010, 0.03])
index = np.argsort(a)[::-1]
b = a[index]
[ 2. 0.5 0.08 0.03 0.012 0.01 ]
Jose Gomez-Dans wrote:
> I've done this with loops, but I am sure there is a much nicer way of
> it with a couple of index tricks.
> Let's say I've got an array of numbers. I want to convert it into an array
> where each element is the rank of that element in the starting array (by
> rank I mean its position when sorted in decreasing order)
> For example, if my original array is
> [ 0.012, 0.08, 2, 0.5, 0.010, 0.03]
> my output array ought to look like this (starting at 1, rather than 0)
> [ 5, 3, 1, 2, 6, 4 ]
> meaning "the first element of the array is the 5th largest, the second is
> the 3rd largest, the third is the largest" and so on.
> The ordering can be done with argsort[::-1] (to get the decreasing order),
> but to get the final array, I can only think of clumsy ways of doing
> Any ideas?
> SciPy-User mailing list
View this message in context: http://old.nabble.com/Ranking-a-list-of-numbers-tp28768184p28770319.html
Sent from the Scipy-User mailing list archive at Nabble.com.
More information about the SciPy-User