[Numpy-discussion] Matrix vs array in ma.minimum

Pierre GM pgmdevlist at gmail.com
Thu Jan 14 07:22:02 EST 2010

On Jan 14, 2010, at 4:53 AM, David Cournapeau wrote:
> Hi,
> I encountered a problem in matlab which boils down to a surprising
> behavior of np.ma.minimum:
> x = np.random.randn(2, 3)
> mx = np.matrix(x)
> np.ma.minimum(x) # smallest item of x
> ret = np.ma.minimum(mx) # flattened version of mx, i.e.  ret == mx.flatten()
> Is this expected ?

Er, no.
np.ma.minimum(a, b) returns the lowest value of a and b element-wsie, or the the lowest element of a is b is None. The behavior is inherited from the very first implementation of maskedarray in numeric. This itself is unexpected, since np.minimum requires at least 2 input arguments.

As you observed, the current function breaks down w/ np.matrix objects when only one argument is given (and when the axis is None): we call umath.minimum.reduce on the ravelled matirx, which returns the ravelled matrix. One would expect a scalar, so yes, this behavior is also unexpected.

Now, which way should we go ? Keep np.ma.minimum as it is (fixing the bug so that a scalar is returned if the function is called with only 1 argument and an axis  None) ? Adapt it to match np.minimum ?

More information about the NumPy-Discussion mailing list