[Numpy-discussion] Question about masked arrays

Benjamin Root ben.root@ou....
Mon Sep 20 15:34:48 CDT 2010

On Mon, Sep 20, 2010 at 3:23 PM, Gökhan Sever <gokhansever@gmail.com> wrote:

> On Mon, Sep 20, 2010 at 1:05 PM, Robert Kern <robert.kern@gmail.com>wrote:
>> Are you asking about when masked arrays are casted to ndarrays (and
>> thus losing the mask information)? Most times when a function uses
>> asarray() or array() to explicitly cast the inputs to an ndarray. The
>> reason that np.mean() gives the same result as np.ma.mean() is that it
>> simply defers to the .mean() method on the object, so it works as
>> expected on a masked array. Many other functions will not.
>> --
>> Robert Kern
> Right guess. It is important for me to able to preserve masked array
> properties of an array. Otherwise losing the mask information yields
> unexpected results in some of my calculations. I could see from np.mean??
> that mean function is indeed the object method. Also in /numpy/ma there is a
> conversion for np.zeros(). I guess in any case it is the user's
> responsibility to make sure that the operations are performed on a desired
> array type.

I have been using masked arrays quite extensively.  My take on them is that
if a masked array makes sense in that operation, then they should still work
with the regular functions.  However, there have been many cases where a
developer used np.asarray() instead of np.asanyarray() for their code, which
causes the masked array object to lose the mask.  If you encounter such
situations, it is usually a bug and should be reported.

Actually, that reminds me... watch out for np.polyfit() with masked arrays.
It doesn't behave quite nicely with masked arrays and the results are
deceptive.  It may appear to be right, but it is not.  Use np.ma.polyfit().

Ben Root
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20100920/c4ddc0d4/attachment.html 

More information about the NumPy-Discussion mailing list