Reputation: 4179
I'm working with numpy masked arrays and there is a trivial operation I cannot figure out how to do it in a simple way. If I have two masked arrays, how can I get them aggregated into another array that contains only the unmasked values?
In [1]: import numpy as np
In [2]: np.ma.array([1, 2, 3], mask = [0,1,1])
Out[2]:
masked_array(data = [1 -- --],
mask = [False True True],
fill_value = 999999)
In [3]: np.ma.array([4, 5, 6], mask = [1,1,0])
Out[3]:
masked_array(data = [-- -- 6],
mask = [ True True False],
fill_value = 999999)
Which operation should I apply to the previous arrays if I want to get:
masked_array(data = [1 -- 6],
mask = [False True False],
fill_value = 999999)
Upvotes: 1
Views: 195
Reputation: 250961
Stack the masks and the arrays using numpy.dstack
and create a new masked array and then you can get the required output using numpy.prod
:
>>> a1 = np.ma.array([1, 2, 3], mask = [0,1,1])
>>> a2 = np.ma.array([7, 8, 9], mask = [1,1,0])
>>> arr = np.ma.array(np.dstack((a1, a2)), mask=np.dstack((a1.mask, a2.mask)))
>>> np.prod(arr[0], axis=1)
masked_array(data = [1 -- 9],
mask = [False True False],
fill_value = 999999)
Upvotes: 1