[Numpy-discussion] RE: default axis for numarray

Konrad Hinsen hinsen at cnrs-orleans.fr
Wed Jun 12 11:11:15 EDT 2002


Paul Barrett <Barrett at stsci.edu> writes:

> I think consistency is an issue, particularly for novices.  You cite
...

Finally a contribution that I can fully agree with :-)

> I don't have enough experience to definitely say whether axis=0 should
> be preferred over axis=-1 or vice versa. But is does appear that for
> the most general cases axis=0 is probably preferred.  This is the
> default for the APL and J programming of which Numeric is based.
> Should we not continue to follow their lead?  It might be nice to see

This the internal logic I referred to briefly earlier, but I didn't
have the time to explain it in more detail. Now I have :-)

The basic idea is that an array is seen as an array of array values.
The N dimensions are split into two parts, the first N1 dimensions
describe the shape of the "total" array, and the remaining N2=N-N1
dimensions describe the shape of the array-valued elements of the
array.

I suppose some examples will help:

- A rank-1 array could be seen either as a vector of scalars
  (N1 = 1) or as a scalar containing a vector (N1 = 0), in
  practice there is no difference between these views.

- A rank-2 array could be seen as a matrix (N1=2), as a vector
  of vectors (N1=1) or as a scalar containing a matrix (N1=0).
  The first and the last come down to the same, but the middle
  one doesn't.

- A discretized vector field (i.e. one 3D vector value for each point
  on a 3D grid) is represented by a rank-6 array, with N1=3 and N2=3.

Array operations are divided into two classes, "structural" and
"element" operations. Element operations do something on each
individual element of an array, returning a new array with the same
"outer" shape, although the element shape may be different. Structural
operations work on the outer shape, returning a new array with a
possibly different outer shape but the same element shape.

The most frequent element operations are addition, multiplication,
etc., which work on scalar elements only. They need no axis argument
at all.

Element operations that work on rank-1 elements have a default axis of
-1, I think FFT has been quoted as an example a few times. There are
no element operations that work on higher-rank elements, but they are
imaginable. A 2D FFT routine would default to axis=-2.

Structural operations, which are by far the most frequent after scalar
element operations, default to axis=0. They include reduction and
accumulation, sorting, selection (take, repeat, ...) and some others.

I hope this clarifies the choice of default axis arguments in the
current NumPy. It is most definitely not arbitrary or accidental. If
you follow the data layout principles explained above, you always
never need to specify an explicit axis argument.

Konrad.
-- 
-------------------------------------------------------------------------------
Konrad Hinsen                            | E-Mail: hinsen at cnrs-orleans.fr
Centre de Biophysique Moleculaire (CNRS) | Tel.: +33-2.38.25.56.24
Rue Charles Sadron                       | Fax:  +33-2.38.63.15.17
45071 Orleans Cedex 2                    | Deutsch/Esperanto/English/
France                                   | Nederlands/Francais
-------------------------------------------------------------------------------




More information about the NumPy-Discussion mailing list