[Numpy-discussion] Strange behavior for argsort() and take()

Thomas J. Duck tom.duck at dal.ca
Wed Jun 18 12:57:56 EDT 2008



      Thanks, Anne.  I misinterpreted what argsort() provides.  I was  
thinking about it in terms of the kind of behaviour exhibited by  
searchsorted().  --Tom



--

On 18-Jun-08, at 12:10 PM, Anne Archibald wrote:

> 2008/6/18 Thomas J. Duck <tom.duck at dal.ca>:
>
>>      I have found what I think is some strange behavior for argsort
>> () and take().  First, here is an example that works as expected:
>>
>>>>> x = numpy.array([1,0,3,2])
>>>>> x.argsort()
>>      array([1, 0, 3, 2])
>>
>> argsort() returns the original array, which is self-indexing for
>> numbers 0 through 3...
>>
>>>>> x.take(x.argsort())
>>      array([0, 1, 2, 3])
>>
>> ...and take() provides the correct sort with ascending order.
>>
>> Now check out what happens when we swap the middle two numbers:
>>
>>>>> x = numpy.array([1,3,0,2])
>>>>> x.argsort()
>>      array([2, 0, 3, 1])
>>
>> argsort() appears to have indexed the numbers in descending order
>> (which was not expected)....
>>
>>>>> x.take(x.argsort())
>>      array([0, 1, 2, 3])
>>
>> ... but the take() operation puts them in ascending order anyway
>> (which was really not expected)!
>>
>>      Can anyone shed some light on what is happening here for me?  I
>> have tested this on both my OS X/Fink and Debian/Lenny systems with
>> the same result.
>
> This is the intended behaviour. The array returned by np.argsort() is
> designed to be used as an index into the original array (take() does
> almost the same thing as indexing). In particular, when x.argsort()
> returns array([2,0,3,1]), that means that the sorted array is x[2],
> x[0], x[3], x[1]. It might be clearer to sort a different array:
>
> In [3]: x = np.array([0.1,0.3,0.0,0.2])
>
> In [5]: x.argsort()
> Out[5]: array([2, 0, 3, 1])
>
> In [7]: x.take(x.argsort())
> Out[7]: array([ 0. ,  0.1,  0.2,  0.3])
>
> If you would like to think of it more mathematically, when you feed
> np.argsort() an array that represents a permutation of the numbers
> 0,1,...,n-1, you get back the inverse permutation. When you pass a
> permutation as the argument to x.take(), you apply the permutation to
> x. (You can also invert a permutation by feeding it as indices to
> arange(n)).
>
> I have been tempted to write some support functions for manipulating
> permutations, but I'm not sure how generally useful they would be.
>
> Anne
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion at scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion

--

Thomas J. Duck <tom.duck at dal.ca>

Associate Professor,
Department of Physics and Atmospheric Science, Dalhousie University,
Halifax, Nova Scotia, Canada, B3H 3J5.
Tel: (902)494-1456 | Fax: (902)494-5191 | Lab: (902)494-3813
Web: http://aolab.phys.dal.ca/





More information about the NumPy-Discussion mailing list