I noticed that the numpy masked-array mean method returns different types when it probably should not:
import numpy as np
A = np.ma.masked_equal([1,1,0], value=0)
B = np.ma.masked_equal([1,1,1], value=0) # no masked values
type(A.mean())
#numpy.float64
type(B.mean())
#numpy.ma.core.MaskedArray
Other numpy.ma.core.MaskedArray
methods seem to be consistent
type( A.sum()) == type(B.sum())
# True
type( A.prod()) == type(B.prod())
# True
type( A.std()) == type(B.std())
# True
type( A.mean()) == type(B.mean())
# False
Can someone explain this?
UPDATE: As pointed out in the comments
C = np.ma.masked_array([1, 1, 1], mask=[False, False, False])
type(C.mean()) == type(A.mean())
# True
B.mask
starts with:np.ma.nomask
isFalse
.This is the case for your
B
:For
A
the mask is an array that matches thedata
in size. InB
it is a scalar,False
, andmean
is handling that as a special case.I need to dig a bit more to see what this implies.
I'm not sure that helps; there's some circular referencing between
np.ndarray
methods and thenp
function and thenp.ma
methods, that makes it hard to identify exactly what code is being used. It like it is using the compiledmean
method, but it isn't obvious how that handles the masking.I wonder if the intent is to use
and the
super
method fetch isn't the right approach.In any case, the same array, but with a vector mask returns the scalar.
====================
Trying this method without the
nomask
shortcut, raises an error afterself.count
returns a scalar in thenomask
case, but anp.int32
in the regular masking. So thecnt.shape
chokes.trace
is the only other masked method that tries thissuper(MaskedArray...)
'shortcut'. There's clearly something kludgy about the mean code.====================
Relevant bug issue: https://github.com/numpy/numpy/issues/5769
According to that the same question was raised here last year: Testing equivalence of means of Numpy MaskedArray instances raises attribute error
Looks like there are a lot of masking issues, not just with
mean
. There may be fixes in the development master now, or in the near future.