Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort NumPy float array column by column

Tags:

python

numpy

Following this trick to grab unique entries for a NumPy array, I now have a two-column array, basically of pairs with first element in the range [0.9:0.02:1.1] and the second element in the range [1.5:0.1:2.0]. Let's call this A. Currently, it's completely unsorted, i.e.

In [111]: A
Out[111]: 
array([[ 1.1 ,  1.9 ],
       [ 1.06,  1.9 ],
       [ 1.08,  1.9 ],
       [ 1.08,  1.6 ],
       [ 0.9 ,  1.8 ],
       ...
       [ 1.04,  1.6 ],
       [ 0.96,  2.  ],
       [ 0.94,  2.  ],
       [ 0.98,  1.9 ]])

I'd like to sort it so that each row first increases in the second column, then the first. i.e.

array([[ 0.9 ,  1.5 ],
       [ 0.9 ,  1.6 ],
       [ 0.9 ,  1.7 ],
       [ 0.9 ,  1.9 ],
       [ 0.9 ,  1.9 ],
       [ 0.9 ,  2.  ],
       [ 0.92,  1.5 ],
       ...
       [ 1.08,  2.  ],
       [ 1.1 ,  1.5 ],
       [ 1.1 ,  1.6 ],
       [ 1.1 ,  1.7 ],
       [ 1.1 ,  1.8 ],
       [ 1.1 ,  1.9 ],
       [ 1.1 ,  2.  ]])

but I can't find a sort algorithm that gives both. As suggested here, I've tried A[A[:,0].argsort()] and A[A[:,1].argsort()], but they only sort one column each. I've also tried applying both but the same thing happens.

I apologize if I've missed something simple but I've been looking for this for a while now...

like image 960
Warrick Avatar asked Sep 19 '12 14:09

Warrick


People also ask

How do I sort a NumPy array based on a column?

NumPy arrays can be sorted by a single column, row, or by multiple columns or rows using the argsort() function. The argsort function returns a list of indices that will sort the values in an array in ascending value.

How do I sort a 2D array by a column?

To column-wise sort a 2D Array in Java, call the “Arrays. sort()” method with a “Comparator interface”. A Comparator interface defines a “compare()” method that accepts two parameters and then compares them with each other. If the passed parameters are equal, it returns zero.


2 Answers

numpy.lexsort will work here:

A[np.lexsort(A.T)]

You need to transpose A before passing it to lexsort because when passed a 2d array it expects to sort by rows (last row, second last row, etc).

The alternative possibly slightly clearer way is to pass the columns explicitly:

A[np.lexsort((A[:, 0], A[:, 1]))]

You still need to remember that lexsort sorts by the last key first (there's probably some good reason for this; it's the same as performing a stable sort on successive keys).

like image 66
ecatmur Avatar answered Oct 06 '22 00:10

ecatmur


the following will work, but there might be a faster way:

A = np.array(sorted(A,key=tuple))
like image 43
mgilson Avatar answered Oct 06 '22 00:10

mgilson