Hi all, I have just lost some time to find a bug related to the fact, that argsort does not preserve the order of an array that is already sorted, see the example below. For me, it would be sufficient to mention this fact in the docstring, although having order preserving argsort is also an option :). What do the developers think? In [33]:a = nm.zeros( 10000 ) In [34]:b = nm.arange( 10000 ) In [35]:nm.alltrue( nm.argsort( a ) == b ) Out[35]:False r.
Robert Cimrman wrote:
Hi all,
I have just lost some time to find a bug related to the fact, that argsort does not preserve the order of an array that is already sorted, see the example below. For me, it would be sufficient to mention this fact in the docstring, although having order preserving argsort is also an option :). What do the developers think?
In [33]:a = nm.zeros( 10000 ) In [34]:b = nm.arange( 10000 ) In [35]:nm.alltrue( nm.argsort( a ) == b ) Out[35]:False
You want a "stable" sorting algorithm like the "mergesort". Use the argsort method with the mergesoret kind option: a.argsort(kind='merge') -Travis
Travis Oliphant wrote:
Robert Cimrman wrote:
I have just lost some time to find a bug related to the fact, that argsort does not preserve the order of an array that is already sorted, see the example below. For me, it would be sufficient to mention this fact in the docstring, although having order preserving argsort is also an option :). What do the developers think?
In [33]:a = nm.zeros( 10000 ) In [34]:b = nm.arange( 10000 ) In [35]:nm.alltrue( nm.argsort( a ) == b ) Out[35]:False
You want a "stable" sorting algorithm like the "mergesort". Use the argsort method with the mergesoret kind option:
a.argsort(kind='merge')
Thank you, Travis. Now I see that the function argsort in oldnumeric.py has different docstring that the array method argsort, which mentions the 'kind' keyword argument. Is the argsort function going to be deprecated? If no, is it possible to synchronize the docstrings? Also a note (in docs) which algorithm is stable would be handy. regards, r.
Robert, Argsort doesn't preserve order by default because quicksort is not a stable sort. Try using the kind="merge" option and see what happens. Or try lexsort, which is targeted at just this sort of sort and uses merge sort. See the documentation here. http://scipy.org/Numpy_Example_List#head-9f8656795227e3c43e849c6c0435eeeb32a... Chuck PS: The function argsort doesn't seem to support this extension in the version I am using (time for another svn update), so you may have to do something like
a = empty(50) a.argsort(kind="merge") array([48, 47, 46, 0, 1, 49, 37, 12, 22, 38, 11, 2, 10, 36, 40, 25, 18, 6, 17, 4, 3, 20, 24, 43, 33, 9, 7, 35, 32, 8, 23, 21, 5, 28, 31, 30, 29, 26, 27, 19, 44, 13, 14, 15, 34, 39, 41, 42, 16, 45])
On 6/8/06, Robert Cimrman <cimrman3@ntc.zcu.cz> wrote:
Hi all,
I have just lost some time to find a bug related to the fact, that argsort does not preserve the order of an array that is already sorted, see the example below. For me, it would be sufficient to mention this fact in the docstring, although having order preserving argsort is also an option :). What do the developers think?
In [33]:a = nm.zeros( 10000 ) In [34]:b = nm.arange( 10000 ) In [35]:nm.alltrue( nm.argsort( a ) == b ) Out[35]:False
r.
_______________________________________________ Numpy-discussion mailing list Numpy-discussion@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/numpy-discussion
Robert, Modifying your example gives In [3]: import numpy as nm In [4]: a = nm.zeros( 10000 ) In [5]: b = nm.arange( 10000 ) In [6]: nm.alltrue( a.argsort(kind="merge" ) == b ) Out[6]: True On 6/8/06, Robert Cimrman <cimrman3@ntc.zcu.cz> wrote:
Hi all,
I have just lost some time to find a bug related to the fact, that argsort does not preserve the order of an array that is already sorted, see the example below. For me, it would be sufficient to mention this fact in the docstring, although having order preserving argsort is also an option :). What do the developers think?
In [33]:a = nm.zeros( 10000 ) In [34]:b = nm.arange( 10000 ) In [35]:nm.alltrue( nm.argsort( a ) == b ) Out[35]:False
r.
_______________________________________________ Numpy-discussion mailing list Numpy-discussion@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/numpy-discussion
participants (3)
-
Charles R Harris -
Robert Cimrman -
Travis Oliphant