# [Numpy-discussion] matrix operations with axis=None

Ed Schofield schofield at ftw.at
Thu Apr 27 09:50:03 CDT 2006

```Travis Oliphant wrote:
> Keith Goodman wrote:
>> I noticed that the mean of a matrix is a matrix but the standard
>> deviation of a matrix is an array. Is that the expected behavior? I'm
>> also getting the wrong values (0 and nan) for the standard deviation.
>> Did I mess something up?
> This should be fixed now in SVN.  If somebody can add a test that
> would be great.
>
> Note, that the methods taking axes also now preserve row and column
> orientation for matrices.
>
Well done for doing this.

In fact, you beat me to it by a few hours; I was going to post a patch
this morning to preserve orientation with matrix operations.  The
approach I took was different in one respect.

Matrix objects currently return a matrix of shape (1, 1) from methods
with an axis=None argument.  For example:

>>> x = asmatrix(random.uniform(0,1,(3,3)))
>>> x.std()
matrix([[ 0.26890557]])

>>> x.argmax()
matrix([[4]])

I believe this behaviour is unfortunate, and that an operation
aggregating a matrix over all dimensions should return a scalar.  I've
posted a patch at

http://projects.scipy.org/scipy/numpy/ticket/83

that modifies this behaviour to return scalars (as rank-0 arrays)
instead.  It also removes some code duplication.

The behaviour with the patch is:

>>> x.std()
0.29610630190701492

>>> x.std().shape
()

>>> x.argmax()
3

Returning scalars from methods with an axis=None argument is the current
behaviour of scipy sparse matrices, while axis=0 or axis=1 yields a
sparse matrix with height or width 1, like numpy matrices.  A (1 x 1)
sparse matrix would be a strange object indeed, and would not be usable
in all contexts where scalars are expected.  I suspect the same would
hold for (1 x 1) dense matrices.  One example is that they cannot be
used as indices for Python lists.  For some matrix methods, such as
argmax, returning a scalar would be highly desirable by allowing simpler
code.

A potential drawback to this change is that matrix operations
aggregating along all dimensions, which would now share the behaviour of
numpy arrays, would be no longer be consistent with matrix operations
that aggregate along only one dimension, which currently do not reduce
dimension, because matrices are inherently 2-d.  This could be an
argument for introducing a new vector class to represent one-dimensional
data with orientation.

-- Ed

```