[PYTHON MATRIX-SIG] Default axis

Jim Hugunin hugunin@mit.edu
Tue, 13 Aug 1996 11:28:31 -0400

Konrad Hinsen wrote:
>    Several people how now brought up the issue of the default axis in the =
>    various functions in NumPy. The inconsistency of this definition is =
>    really a problem.  I think that it's really important to have a =
>    consistent default axis, and I propose that the default axis be made -1 =
>    (or -2 for things that expect 2d arrays).  This works fairly well for me =
> The matter is really a bit more complicated. Basically, the problem
> occurs for arrays whose indices should logically be divided into
> two sections: a "superarray" with elements that are subarrays (in J these
> are called frames and cells, respectively). There are operations
> that act on the superarray, like reduction, and the best default axis
> for those is 0. Other operations act on cells (e.g. 1D FFT), and their
> default axis should be -1. (In J the first type of function would have
> infinite default rank, and the second one would have default rank 1).
> The distinction makes sense in practice and I am inclilned to keep it.
> Nobody will be happy with a setup in which you almost always have to
> specify an axis explicitly due to a bad default value.
> Konrad.

I find myself almost always typing add.reduce(a, -1).  What I mean by
this function is to do the reduction along all of the vectors in my

Why is it unreasonable to assume that functions like add.reduce act on
the cells of the array?

As far as sort is concerned, this function only makes sense to me as
operating on 1d arrays (where scalars are the elements to be sorted) if
you want to sort anything else then you need to define a python
comparison function, and at that point you might as well just write the
whole thing as python code.

Since sort naturally applies only to 1d arrays, I think its default axis
should be -1.

Now concatenate is another story entirely, and my current thoughts there
are to leave axis=0 as its default.  This will be the one exception to
the axis=-1 rule.  I also disagree that concatenate should broadcast its
arguments.  should concatenate([[1,2,3],[4,5,6]], 0) ->

I certainly hope not!

on the other hand, to steal from another post:

Carlos Fonseca wrote:
> >>> a
>  0  1  2  3
>  4  5  6  7
>  8  9 10 11
> >>> b
> 0 1 2 3
> Since these arrays are aligned in the usual sense (a+b would work),
> one would expect concatenate((a,b)) to work by default. This means
> axis=0. What do others think?
> Actually, the above would not work in the present implementation if
> b.shape=(4,), but one would have to write concatenate((a,b[NewAxis,:])).

What you have to write in the current implementation is concatenate( (a,
[b]) ).  This to me is not too much work to make sure that the user
understands what s/he's doing, and this is perfectly in line with the
notion of concatentation found in the rest of python.


MATRIX-SIG  - SIG on Matrix Math for Python

send messages to: matrix-sig@python.org
administrivia to: matrix-sig-request@python.org