[Numpy-discussion] matrix operations with axis=None

Travis Oliphant oliphant at ee.byu.edu
Thu Apr 27 10:01:07 CDT 2006


Ed Schofield wrote:

>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.
>  
>

I like your function-call approach as it ensures consistent behavior.

>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.
>  
>

There is one more problem in that matrix-operations will not be 
preserved in all cases as they would have before.

However, I suppose somebody doing a reduce over all dimensions would 
probably not expect the result to be a matrix, so I don't think it's a 
big drawback.

Consistency with sparse matrices is another reason for returning a scalar.

-Travis





More information about the Numpy-discussion mailing list