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?
d = [a,b,c]instead of appending.