
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]])

On Wed, May 12, 2010 at 20:19, Dr. Phillip M. Feldman pfeldman@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-2 wrote:
On Wed, May 12, 2010 at 20:19, Dr. Phillip M. Feldman pfeldman@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.
Phillip

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@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? 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.
Eric
Phillip

On Fri, May 14, 2010 at 17:29, Eric Firing efiring@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@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.

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.
not to hammer a point home (OK, it IS to hammer a point home), this is one of the things that drove me crazy about MATLAB -- everything was a 2-d array, unless one dimension happened to have length 1, and then some (but not all) functions treated it as 1-d. I had to write me own version of sum() for instance, that would sum over the last dimension, even if it happened to be 1.
numpy provides n-d arrays, so you don't have that silliness -- if you want a 1-d array, use a 1-d array.
I can't find it right now, but I'm pretty sure there is a function that will re-shape an array to remove the length-1 dimensions -- maybe that's what the OP needs.
-Chris

Chris Barker wrote:
I can't find it right now, but I'm pretty sure there is a function that will re-shape an array to remove the length-1 dimensions -- maybe that's what the OP needs.
it's np.squeeze()
-Chris

efiring wrote:
On 05/14/2010 11:03 AM, Dr. Phillip M. Feldman wrote:
<snip> 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.
Eric
Excellent point! I agree. Case closed.
Phillip
participants (4)
-
Chris Barker
-
Dr. Phillip M. Feldman
-
Eric Firing
-
Robert Kern