From: Charles R H. <cha...@gm...> - 2006-10-23 03:22:24
|
On 10/22/06, Albert Strasheim <fu...@gm...> wrote: > > Hello all > > I'm trying to sort an array with two fields, but I'm getting a result that > doesn't seem to make sense. > > What I tried (first attempt): I have two 2-D arrays. I would like to sort > one based on the sort of the other. I managed to do this with argsort. > However, the fancy indexing required to get the sorted array using what > argsort returned was very slow. I followed this example: It is certainly awkward. I am going to add a function to do this after 1.0comes out. Sounds like you need it now. http://www.scipy.org/Numpy_Example_List#head-9f8656795227e3c43e849c6c0435eee > b32afd722 > > What I tried (second attempt): I created an array with two fields. I > think/hope/expected that sorting the array would sort it on the first > field > in the dtype and then on the second. This is *much* faster than the fancy > indexing approach. I believe it sorts on the two fields together as one big binary blob. <snip> Output on my system: > > 1.0.dev3376 > > before sort: > [[(1.0, 1) (2.0, 2)] > [(3.0, 3) (4.0, 4)]] > > after sort: > [[(2.0, 2) (1.0, 1)] > [(4.0, 4) (3.0, 3)]] > > The already sorted array has been unsorted in some way. Any thoughts? I suspect something to do with the binary representation of the floats. Probably depends on big/little endian also. The floats 1.0, 2.0, and 4.0all have zero mantissas while 3.0 has a one. The sort behaves differently if uint8 is used for both fields. In [3]: dt = dtype([('aaa', uint8), ('bbb', uint8)]) In [4]: x = empty((2, 2), dt) In [5]: xa = x[dt.names[0]] In [6]: xa[:] = [[1,2], [3,4]] In [7]: xb = x[dt.names[1]] In [8]: xb[:] = [[1,2], [3,4]] In [9]: x.sort() In [11]: x Out[11]: array([[(1, 1), (2, 2)], [(3, 3), (4, 4)]], dtype=[('aaa', '|u1'), ('bbb', '|u1')]) Chuck |