[Numpy-discussion] Question about masked arrays

Robert Kern robert.kern@gmail....
Mon Sep 20 13:05:26 CDT 2010


On Sun, Sep 19, 2010 at 12:19, Gökhan Sever <gokhansever@gmail.com> wrote:
> Hello,
> Consider these two sets of container arrays --one defined as usual np array
> the others as ma arrays:
>     all_measured = np.ma.zeros((16, 18))
>     all_predicted = np.ma.zeros((16, 18))
>     all_measured2 = np.zeros((16, 18))
>     all_predicted2 = np.zeros((16, 18))
> I do a computation within a for loop to assign 16 set of measurements into a
> length of 18 arrays (thus constructing a 2D array to perform overall
> statistics and plotting.) For the simplicity I only show a portion of
> all_measured and all_measured2 as:
> all_measured
> masked_array(data =
>  [[512.632175 527.33373 565.36541 567.53967 593.86833 570.31319 574.40965
>   582.72649 588.21336 618.48789 593.09007 620.33474 591.10203 611.06443
>   655.60614 638.13193 626.71769 625.63584]
>  [626.6435 -- -- 1183.67671 1206.82453 1183.13248 1162.5514 1180.70062
>   1086.53246 1078.78711 997.1642 856.57159 645.35167 696.86947 778.40914
>   816.03059 862.88297 901.7237] ...
> all_measured2
> array([[  512.632175  ,   527.33373   ,   565.36541   ,   567.53967   ,
>           593.86833   ,   570.31319   ,   574.40965   ,   582.72649   ,
>           588.21336   ,   618.48789   ,   593.09007   ,   620.33474   ,
>           591.10203   ,   611.06443   ,   655.60614   ,   638.13193   ,
>           626.71769   ,   625.63584   ],
>        [  626.6435    ,     0.        ,     0.        ,  1183.67671   ,
>          1206.82453   ,  1183.13248   ,  1162.5514    ,  1180.70062   ,
>          1086.53246   ,  1078.78711   ,   997.1642    ,   856.57159   ,
>           645.35167   ,   696.86947   ,   778.40914   ,   816.03059   ,
>           862.88297   ,   901.7237    ],...
> The issue is why masked arrays casted to regular numpy arrays as in
> all_measured2 case? whereas a simple numpy function np.mean and ma
> equivalent np.ma.mean yields same results on all_measured? Because the
> former requires a priori knowledge about the type of arrays, however the
> latter doesn't necessitate such restriction.
> Hope this is clear. Thanks.

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

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
  -- Umberto Eco


More information about the NumPy-Discussion mailing list