Sorting Arrays in Numpy by Column

Sorting arrays in NumPy by column

@steve's answer is actually the most elegant way of doing it.

For the "correct" way see the order keyword argument of numpy.ndarray.sort

However, you'll need to view your array as an array with fields (a structured array).

The "correct" way is quite ugly if you didn't initially define your array with fields...

As a quick example, to sort it and return a copy:

In [1]: import numpy as np

In [2]: a = np.array([[1,2,3],[4,5,6],[0,0,1]])

In [3]: np.sort(a.view('i8,i8,i8'), order=['f1'], axis=0).view(np.int)
Out[3]:
array([[0, 0, 1],
[1, 2, 3],
[4, 5, 6]])

To sort it in-place:

In [6]: a.view('i8,i8,i8').sort(order=['f1'], axis=0) #<-- returns None

In [7]: a
Out[7]:
array([[0, 0, 1],
[1, 2, 3],
[4, 5, 6]])

@Steve's really is the most elegant way to do it, as far as I know...

The only advantage to this method is that the "order" argument is a list of the fields to order the search by. For example, you can sort by the second column, then the third column, then the first column by supplying order=['f1','f2','f0'].

numpy sort arrays based on last column values

ind=np.argsort(a[:,-1])
b=a[ind]

EDIT
When you use axis in the sort, it sorts every column individually, what you want is to get indices of the sorted rows from the selected column (-1 is equivalent to the last column), and then reorder your original array.

How to sort numpy array column-wise consequetly?

This should work

import numpy as np

a = np.array([[1, 0, 4, 2, 3],[0, 1, 5, 7, 4],[0, 0, 6, 1, 0]])

np.sort(a.view('i8,i8,i8,i8,i8'), order=['f0'], axis=0).view(np.int)

I get

array([[0, 0, 6, 1, 0],
[0, 1, 5, 7, 4],
[1, 0, 4, 2, 3]])

f0 is the column which you want to sort by.

How to sort a 2d numpy array by the columns with the biggest sum

I'm not sure if your solution is incorrect, but it is certainly more complicated than necessary:

>>> a = np.array([[5, 3, 13],
[1, 2, 20],
[6, 2, 6]])

>>> a[:, a.sum(axis=0).argsort()] # sort columns small-to-large
array([[ 3, 5, 13],
[ 2, 1, 20],
[ 2, 6, 6]])

>>> a[:, (a.sum(axis=0)*-1).argsort()] # multiply sums by -1 to sort large-to-small
array([[13, 5, 3],
[20, 1, 2],
[ 6, 6, 2]])

Descending sorting in numpy by several columns

Use numpy.lexsort to sort on multiple columns at the same time.

arr = np.array([
[150, 8],
[105, 20],
[90, 100],
[101, 12],
[110, 80],
[105, 100],
])

order = np.lexsort([arr[:, 1], arr[:, 0]])[::-1]
arr[order]

yields:

array([[150,   8],
[110, 80],
[105, 100],
[105, 20],
[101, 12],
[ 90, 100]])

Sorting arrays in NumPy ascending on one column and descending on other column

You can use the np.lexsort function for this

import numpy as np
a = np.asarray([[9, 2, 3],
[4, 5, 6],
[7, 0, 5],
[7, 1, 6]])

a[np.lexsort((-a[:, 1], a[:, 0]))]

Output

array([[4, 5, 6],
[7, 1, 6],
[7, 0, 5],
[9, 2, 3]])

Sorting a 2by2 Numpy array by one column in descending order


import numpy as np
this = {"z": 1.6, "Sorting Arrays in Numpy by Columnaaaaa": 0, "w": 6, "v": 4}
array=np.array([[key,val] for (key,val) in this.items()])
sortedArr = array[array[:,1].argsort()[::-1]]
print(sortedArr)

Sort 2d numpy array based on one column with float numbers

Any ideas on how to solve this?

I used numpy.argsort without problem as follows:

import numpy as np
arr = np.array([[1.0,1.0,5.3],[1.0,2.0,4.7],[1.0,3.0,3.5]])
sorted_arr = arr[np.argsort(arr[:, -1])]
print(sorted_arr)

output

[[1.  3.  3.5]
[1. 2. 4.7]
[1. 1. 5.3]]

In my example dtype is float64. Please check if dtype of your array is actually some float, by doing print(your_array.dtype). Note that numpy.argsort has optional argument kind, default is quicksort, you might give a try different versions and look what will happen.

How to sort a numpy array based on the both arrays and also conditions

You could define a sort function like

sortfunc = my_point[:, 0] * 100 + my_point[:, 1] * 10 - my_point[:, 2]

idx = np.argsort(sortfunc)
my_point[idx]
# array([[ 1. , 0. , 100.1],
# [ 1. , 2. , 90.9],
# [ 2. , 0. , 100. ],
# [ 2. , 1. , 100. ],
# [ 1.8, 0. , 2.8],
# [ 1.8, 1. , 2.7],
# [ 1.8, 2. , 3.1],
# [ 3. , 0. , 3. ],
# [ 3. , 2. , 2.9]])

Of course the coefficients 100, 10, and -1 are then a matter of tuning.



Related Topics



Leave a reply



Submit