[Numpy-discussion] default behavior of argsort

Robert Kern robert.kern at gmail.com
Fri May 14 17:53:41 EDT 2010


On Fri, May 14, 2010 at 17:29, Eric Firing <efiring at hawaii.edu> wrote:
> On 05/14/2010 11:03 AM, Dr. Phillip M. Feldman wrote:
>>
>> Robert Kern-2 wrote:
>>>
>>> On Wed, May 12, 2010 at 20:19, Dr. Phillip M. Feldman
>>> <pfeldman at verizon.net>  wrote:
>>>>
>>>> When operating on an array whose last dimension is unity, the default
>>>> behavior of argsort is not very useful:
>>>>
>>>> |6>  x=random.random((4,1))
>>>> |7>  shape(x)
>>>>                       <7>  (4, 1)
>>>> |8>  argsort(x)
>>>>                       <8>
>>>> array([[0],
>>>>        [0],
>>>>        [0],
>>>>        [0]])
>>>> |9>  argsort(x,axis=0)
>>>>                       <9>
>>>> array([[0],
>>>>        [2],
>>>>        [1],
>>>>        [3]])
>>>
>>> Sorry, but I don't think we are going to add a special case for this.
>>>
>>> --
>>> Robert Kern
>>>
>>
>> I don't see this as a special case.  When axis is unspecified, the default
>> is axis=-1, which causes argsort to operate on the last dimension.  A more
>> sensible default would be the last non-unity dimension.
>
> That would be too clever for my liking.  First, a default should be
> something that can also be explicitly specified; how would you use the
> axis kwarg to specify the last non-unit dimension?

None would be reasonable. Unfortunately, that would be inconsistent
with the interpretation of other axis=None arguments elsewhere in
numpy.

> Second, treating a
> unit dimension differently from a non-unit dimension *is* making it a
> special case, and often--usually--one does not want that.  It is
> perfectly reasonable to have an algorithm that uses values sorted along
> the last axis, even if that dimension sometimes turns out to be one.

Right. Changing behavior on an edge case makes everyone else have to
deal with that edge case in their code.

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
  -- Umberto Eco



More information about the NumPy-Discussion mailing list