[Numpy-discussion] Strange behavior for argsort() and take()
Thomas J. Duck
Wed Jun 18 11:57:56 CDT 2008
Thanks, Anne. I misinterpreted what argsort() provides. I was
thinking about it in terms of the kind of behaviour exhibited by
On 18-Jun-08, at 12:10 PM, Anne Archibald wrote:
> 2008/6/18 Thomas J. Duck <email@example.com>:
>> 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])
>> array([1, 0, 3, 2])
>> argsort() returns the original array, which is self-indexing for
>> numbers 0 through 3...
>> 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])
>> array([2, 0, 3, 1])
>> argsort() appears to have indexed the numbers in descending order
>> (which was not expected)....
>> 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,
> x, x, x. It might be clearer to sort a different array:
> In : x = np.array([0.1,0.3,0.0,0.2])
> In : x.argsort()
> Out: array([2, 0, 3, 1])
> In : x.take(x.argsort())
> Out: 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
> I have been tempted to write some support functions for manipulating
> permutations, but I'm not sure how generally useful they would be.
> Numpy-discussion mailing list
Thomas J. Duck <firstname.lastname@example.org>
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
More information about the Numpy-discussion