Strange results when sorting array with fields

Charles R Harris charlesr.harris at gmail.com
Sun Oct 22 22:22:20 CDT 2006


On 10/22/06, Albert Strasheim <fullung at gmail.com> 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/numpy-discussion/attachments/20061022/0335c2b3/attachment-0001.html 
-------------- next part --------------
-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
-------------- next part --------------
_______________________________________________
Numpy-discussion mailing list
Numpy-discussion at lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/numpy-discussion


More information about the Numpy-discussion mailing list