Re: [Numpy-discussion] is there a sortrows
josef.p...@gmail.com wrote: I was looking for a function that sorts a 2-dimensional array by rows. That's what I came up with, is there a more direct way? a array([[1, 2], [0, 0], [1, 0], [0, 2], [2, 1], [1, 0], [1, 0], [0, 0], [1, 0], [2, 2]]) a[np.lexsort(np.fliplr(a).T)] array([[0, 0], [0, 0], [0, 2], [1, 0], [1, 0], [1, 0], [1, 0], [1, 2], [2, 1], [2, 2]]) Note: I needed to flip and transpose, using axis didn't work a.shape (10, 2) np.lexsort(a,axis=1) Traceback (most recent call last): File pyshell#76, line 1, in module np.lexsort(a,axis=1) ValueError: axis(=1) out of bounds Specifying individual columns in argument also works, but it's a pain if I don't know how many columns there are: a[np.lexsort((a[:,1],a[:,0]))] array([[0, 0], [0, 0], [0, 2], [1, 0], [1, 0], [1, 0], [1, 0], [1, 2], [2, 1], [2, 2]]) A helper function sortrows would be helpful, I don't know what would be the higher dimensional equivalent. Or did I miss a function that I didn't find in the help file? I may miss something obvious, but why are you using lexsort at all ? At leat, the first example is easily achieved with sort(x, axis=0) - but maybe you have more complicated examples in mind where you need actual lexical sort: David ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] is there a sortrows
On Sun, Dec 21, 2008 at 20:53, David Cournapeau da...@ar.media.kyoto-u.ac.jp wrote: josef.p...@gmail.com wrote: I was looking for a function that sorts a 2-dimensional array by rows. That's what I came up with, is there a more direct way? a array([[1, 2], [0, 0], [1, 0], [0, 2], [2, 1], [1, 0], [1, 0], [0, 0], [1, 0], [2, 2]]) a[np.lexsort(np.fliplr(a).T)] array([[0, 0], [0, 0], [0, 2], [1, 0], [1, 0], [1, 0], [1, 0], [1, 2], [2, 1], [2, 2]]) Note: I needed to flip and transpose, using axis didn't work a.shape (10, 2) np.lexsort(a,axis=1) Traceback (most recent call last): File pyshell#76, line 1, in module np.lexsort(a,axis=1) ValueError: axis(=1) out of bounds Specifying individual columns in argument also works, but it's a pain if I don't know how many columns there are: a[np.lexsort((a[:,1],a[:,0]))] array([[0, 0], [0, 0], [0, 2], [1, 0], [1, 0], [1, 0], [1, 0], [1, 2], [2, 1], [2, 2]]) A helper function sortrows would be helpful, I don't know what would be the higher dimensional equivalent. Or did I miss a function that I didn't find in the help file? I may miss something obvious, but why are you using lexsort at all ? At leat, the first example is easily achieved with sort(x, axis=0) No, it isn't. In [4]: sort(a, axis=0) Out[4]: array([[0, 0], [0, 0], [0, 0], [1, 0], [1, 0], [1, 0], [1, 1], [1, 2], [2, 2], [2, 2]]) Compare to his desired result: array([[0, 0], [0, 0], [0, 2], [1, 0], [1, 0], [1, 0], [1, 0], [1, 2], [2, 1], [2, 2]]) -- Robert Kern I have come to believe that the whole world is an enigma, a harmless enigma that is made terrible by our own mad attempt to interpret it as though it had an underlying truth. -- Umberto Eco ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] is there a sortrows
I may miss something obvious, but why are you using lexsort at all ? At leat, the first example is easily achieved with sort(x, axis=0) - but maybe you have more complicated examples in mind where you need actual lexical sort: David From the examples that I tried out np.sort, sorts each column separately (with axis = 0). If the elements of a row is supposed to stay together, then np.sort doesn't work arr array([[ 1, 14], [ 4, 12], [ 3, 11], [ 2, 14]]) np.sort(arr,axis=0) array([[ 1, 11], [ 2, 12], [ 3, 14], [ 4, 14]]) Josef ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] is there a sortrows
josef.p...@gmail.com wrote: I may miss something obvious, but why are you using lexsort at all ? At leat, the first example is easily achieved with sort(x, axis=0) - but maybe you have more complicated examples in mind where you need actual lexical sort: David From the examples that I tried out np.sort, sorts each column separately (with axis = 0). If the elements of a row is supposed to stay together, then np.sort doesn't work. You're right, as Robert just mentioned, I totally missed the point of your example... David ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] is there a sortrows
On Dec 21, 2008, at 10:19 PM, josef.p...@gmail.com wrote: From the examples that I tried out np.sort, sorts each column separately (with axis = 0). If the elements of a row is supposed to stay together, then np.sort doesn't work Well, if the elements are supposed to stay together, why wouldn't you tie them first, sort, and then untie them ? np.sort(a.view([('',int),('',int)]),0).view(int) The first view transforms your 2D array into a 1D array of tuples, the second one retransforms the 1D array to 2D. Not sure it's better than your lexsort, haven't timed it. ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] is there a sortrows
On Sun, Dec 21, 2008 at 11:10 PM, Pierre GM pgmdevl...@gmail.com wrote: On Dec 21, 2008, at 10:19 PM, josef.p...@gmail.com wrote: From the examples that I tried out np.sort, sorts each column separately (with axis = 0). If the elements of a row is supposed to stay together, then np.sort doesn't work Well, if the elements are supposed to stay together, why wouldn't you tie them first, sort, and then untie them ? np.sort(a.view([('',int),('',int)]),0).view(int) The first view transforms your 2D array into a 1D array of tuples, the second one retransforms the 1D array to 2D. Not sure it's better than your lexsort, haven't timed it. That's very helpful, not so much about the sort but it's a good example to move back and forth between structured and regular arrays. My help search for this was not successful enough to figure this out by myself. Several functions require structured arrays but I didn't know how to get them without specifying everything by hand. And when I have a structured array, I didn't know how to call var or mean on them. Your suggestion also works with automatic adjustment for number of columns. np.sort(a.view([('','i4')]*a.shape[1]),0).view(int) Thanks, Josef ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion
Re: [Numpy-discussion] is there a sortrows
On Sun, Dec 21, 2008 at 11:37 PM, josef.p...@gmail.com wrote: On Sun, Dec 21, 2008 at 11:10 PM, Pierre GM pgmdevl...@gmail.com wrote: On Dec 21, 2008, at 10:19 PM, josef.p...@gmail.com wrote: From the examples that I tried out np.sort, sorts each column separately (with axis = 0). If the elements of a row is supposed to stay together, then np.sort doesn't work Well, if the elements are supposed to stay together, why wouldn't you tie them first, sort, and then untie them ? np.sort(a.view([('',int),('',int)]),0).view(int) The first view transforms your 2D array into a 1D array of tuples, the second one retransforms the 1D array to 2D. Not sure it's better than your lexsort, haven't timed it. That's very helpful, not so much about the sort but it's a good example to move back and forth between structured and regular arrays. My help search for this was not successful enough to figure this out by myself. Several functions require structured arrays but I didn't know how to get them without specifying everything by hand. And when I have a structured array, I didn't know how to call var or mean on them. Your suggestion also works with automatic adjustment for number of columns. np.sort(a.view([('','i4')]*a.shape[1]),0).view(int) Thanks, Josef Version with fully automatic conversion, I don't even have to know the dtype np.sort(a.view([('',a.dtype)]*a.shape[1]),0).view(a.dtype) (this is for future Google searches) Josef ___ Numpy-discussion mailing list Numpy-discussion@scipy.org http://projects.scipy.org/mailman/listinfo/numpy-discussion