简体   繁体   中英

How to sort a numpy array based on the values in a specific row?

I was wondering how I would be able to sort a whole array by the values in one of its columns.

I have :

array([5,2,8,2,4])

and:

array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14],
       [15, 16, 17, 18, 19],
       [20, 21, 22, 23, 24]])

I want to append the first array to the second one like this:

array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14],
       [15, 16, 17, 18, 19],
       [20, 21, 22, 23, 24],
       [5,  2,  8,  2,  4]])

And then sort the array by the appended row to get either this:

array([[1,  3,  4,  0,  2],
       [6,  8,  9,  5,  7],
       [11, 13, 14, 10, 12],
       [16, 18, 19, 15, 17],
       [21, 23, 24, 20, 22],
       [2,  2,  4,  5,  8]])

or this:

array([[ 2,  1,  3,  4,  0],
       [ 7,  6,  8,  9,  5],
       [12, 11, 13, 14, 10],
       [17, 16, 18, 19, 15],
       [22, 21, 23, 24, 20],
       [ 8,  5,  4,  2,  2]])

And then remove the appended column to get:

array([[1,  3,  4,  0,  2],
       [6,  8,  9,  5,  7],
       [11, 13, 14, 10, 12],
       [16, 18, 19, 15, 17],
       [21, 23, 24, 20, 22]])

or:

array([[ 2,  1,  3,  4,  0],
       [ 7,  6,  8,  9,  5],
       [12, 11, 13, 14, 10],
       [17, 16, 18, 19, 15],
       [22, 21, 23, 24, 20]])

Is there a code to carry out this procedure. I am very new to python. Thanks a lot!

You can use numpy.argsort to get a list with the sorted indices of your array. Using that you can then rearrange the columns of the matrix.

import numpy as np

c = np.array([5,2,8,2,4])    
a = np.array([[ 0,  1,  2,  3,  4],
              [ 5,  6,  7,  8,  9],
              [10, 11, 12, 13, 14],
              [15, 16, 17, 18, 19],
              [20, 21, 22, 23, 24]])

i = np.argsort(c)
a = a[:,i]

You don't need numpy to do this; (although if you are using numpy, you can just use the .transpose() method of the array class.

What this essentially does, is transpose your array so that it's array[column][row] , and then takes each columns, and pairs them with the sortKeys you provided in a list of tuples (the zip(sortKeys, a) bit). Then it sorts this list of tuples. By default, sort with sort tuples based on their first value, then their second value, then third, etc. Now you have the columns in order.

then aNew = [...] just extracts the columns and creates your new array, still in the array[column][row] format, and then transposes it again.

a = [[ 0,  1,  2,  3,  4],
     [ 5,  6,  7,  8,  9],
     [10, 11, 12, 13, 14],
     [15, 16, 17, 18, 19],
     [20, 21, 22, 23, 24]]

#transpose a
a = zip(*a)

sortKeys = [5,2,8,2,4]

b = zip(sortKeys, a)

aNew = [row[1] for row in sorted(b)]

#transpose a back
aNew = zip(*aNew)

print aNew

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM