1

I am new in python programming, please forgive me if my question is too basic. But I am trying to use masked_array to calculate the mean of three arrays to produce a third array without using the elements with values less than zero. Using these steps:

import numpy as np
from numpy.ma import masked_array

d=[]
a = np.array([[-2,-3,-4,-6],[5,2,6,1],[9,3,2,4],[3,1,1,2]])
b = np.array([[3,4,2,4],[5,2,6,1],[9,3,2,4],[0.3,12,1,3]])
c = np.array([[2,3,4,5],[7,0,1,5],[10,9,2,3],[1.5,2.01,2,0.2]])

mask = (a <= 0).astype(int) 
a = masked_array(a,mask)

d.append(a)
d.append(b)
d.append(c)
result = np.array(d).mean(axis=0)
print result

[[ 1.          1.33333333  0.66666667  1.        ]
[ 5.66666667  1.33333333  4.33333333  2.33333333]
[ 9.33333333  5.          2.          3.66666667]
[ 1.6         5.00333333  1.33333333  1.73333333]]

I thought that (from documentation that) if one uses the numpy array mean function on arrays with masked values, that it will not consider them (the masked values) in computing the mean. I expected the result to be

[[  2.5          3.5  3.  4.5        ]
 [ 5.66666667  1.33333333  4.33333333  2.33333333]
 [ 9.33333333  5.          2.          3.66666667]
 [ 1.6         5.00333333  1.33333333  1.73333333]]

Please, does anyone have some tips on how I can use the numpy.ma.masked_array to archive this?

1
  • 1
    This isn't part of the problem, but you could write d = [a,b,c] instead of appending. Commented Jul 31, 2015 at 15:00

1 Answer 1

1

The problem is that np.array(d) does not create a masked array. The mask in a is lost when the arrays in the list d are assembled into a bigger (nonmasked) array.

One way to fix it is to replace this:

result = np.array(d).mean(axis=0)

with

result = masked_array(d).mean(axis=0)

E.g.:

In [27]: result = masked_array(d).mean(axis=0)

In [28]: result
Out[28]: 
masked_array(data =
 [[2.5 3.5 3.0 4.5]
 [5.666666666666667 1.3333333333333333 4.333333333333333 2.3333333333333335]
 [9.333333333333334 5.0 2.0 3.6666666666666665]
 [1.5999999999999999 5.003333333333333 1.3333333333333333
  1.7333333333333334]],
             mask =
 [[False False False False]
 [False False False False]
 [False False False False]
 [False False False False]],
       fill_value = 1e+20)
Sign up to request clarification or add additional context in comments.

1 Comment

Thank your very much @Warren Weckesser. This work perfectly for this scenario.

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.