2

I have a problem that puzzles me a lot the last days. I want to average different masked arrays but it seems I cannot because my mapped arrays do not have the same map pattern.

For example. I have three arrays:

 [ --  --  --]
 [ 1.  2.  --]
 [ 2.  3.  --]
 [ --  --  --]

 [ --  2.  2.]
 [ --  2.  3.]
 [ --  --  --]
 [ --  --  --]

 [ 2   1.  --]
 [ 1   1.  --]
 [ --  --  --]
 [ --  --  --]

I want the result to be the average of these arrays, but the averaging of a masked element with a valid element should not take account the masked elements. That means in the (0,0) position I have two masked elements and 1 valid ( value =2 ), so the final result should calculate only the average of the valid elements.

 [ 2   1.5  2]
 [ 1   2    3]
 [ 2   --  --]
 [ --  --  --]

I have 28 arrays like them that I want to combine and so far I have to do complicated loops and actions to achieve the expected results. Is there any efficient way to do it?

4
  • 1
    a.mean(axis=0) doesn't work? Commented Nov 14, 2014 at 13:42
  • All the arrays have the same size. Sorry It was in error in array one. Commented Nov 14, 2014 at 13:57
  • @Evert No it does not work, when I am trying for example: (array1+array2+array3).mean(axis=0) then I get only a mean for the elements that are valid in EVERY array. Commented Nov 14, 2014 at 14:03
  • Of course it does work; you just have to take the intermediate step, like Saullo explains below. Commented Nov 14, 2014 at 14:59

1 Answer 1

1

You can transform them into a 3-D masked array and then take the average along axis=0, for example:

np.ma.array((a, b, c)).mean(axis=0)

Example:

import numpy as np

a = np.ma.array([[99, 99, 99],
                 [ 1.,  2., 99],
                 [ 2.,  3., 99],
                 [99, 99, 99]],
                 mask=[[True, True, True],
                       [False, False, True],
                       [False, False, True],
                       [True, True, True]])
b = np.ma.array([[99,  2.,  2.],
                 [99,  2.,  3.],
                 [99, 99, 99],
                 [99, 99, 99]],
                 mask=[[True, False, False],
                       [True, False, False],
                       [True, True, True],
                       [True, True, True]])
c = np.ma.array([[2., 1., 99],
                 [1., 1., 99],
                 [99, 99, 99],
                 [99, 99, 99]],
                 mask=[[False, False, True],
                       [False, False, True],
                       [True, True, True],
                       [True, True, True]])

and:

np.ma.array((a, b, c)).mean(axis=0)

masked_array(data =
 [[2.0 1.5 2.0]
 [1.0 1.6666666666666667 3.0]
 [2.0 3.0 --]
 [-- -- --]],
             mask =
 [[False False False]
 [False False False]
 [False False  True]
 [ True  True  True]],
       fill_value = 1e+20)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Saullo! That works exactly as I would like :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.