2

So I've a 2d array, that when sorted by the second column using a[np.argsort(-a[:,1])] looks like this:

array([[ 30.        ,  98.7804878 ],
       [ 24.        ,  98.7804878 ],
       [ 21.        ,  98.7804878 ],
       [ 26.        ,  98.7804878 ],
       [ 20.        ,  98.70875179],
       [  4.        ,  98.27833572],
       [  1.        ,   7.10186514]])

Now I want to sort this by the lowest "id" column so it looks like this:

array([[ 21.        ,  98.7804878 ],
       [ 24.        ,  98.7804878 ],
       [ 26.        ,  98.7804878 ],
       [ 30.        ,  98.7804878 ],
       [ 20.        ,  98.70875179],
       [  4.        ,  98.27833572],
       [  1.        ,   7.10186514]])

I can't figure out how to do it, even if I take the top highest percentages from the first and then order them.

0

1 Answer 1

7

You can use np.lexsort for this:

>>> a[np.lexsort((a[:, 0], -a[:, 1]))]
array([[ 21.        ,  98.7804878 ],
       [ 24.        ,  98.7804878 ],
       [ 26.        ,  98.7804878 ],
       [ 30.        ,  98.7804878 ],
       [ 20.        ,  98.70875179],
       [  4.        ,  98.27833572],
       [  1.        ,   7.10186514]])

This sorts by -a[:, 1], then by a[:, 0], returning an array of indices than you can use to index a.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.