user308827
user308827

Reputation: 21971

Adding two 2D NumPy arrays ignoring NaNs in them

What is the right way to add 2 numpy arrays a and b (both 2D) with numpy.nan as missing value?

  1. a + b

or

  1. numpy.ma.sum(a,b)

Upvotes: 5

Views: 12306

Answers (2)

MSeifert
MSeifert

Reputation: 152667

Just replace the NaNs with zeros in both arrays:

a[np.isnan(a)] = 0  # replace all nan in a with 0
b[np.isnan(b)] = 0  # replace all nan in b with 0

And then perform the addition:

a + b

This relies on the fact that 0 is the "identity element" for addition.

Upvotes: 6

Divakar
Divakar

Reputation: 221574

Since the inputs are 2D arrays, you can stack them along the third axis with np.dstack and then use np.nansum which would ensure NaNs are ignored, unless there are NaNs in both input arrays, in which case output would also have NaN. Thus, the implementation would look something like this -

np.nansum(np.dstack((A,B)),2)

Sample run -

In [157]: A
Out[157]: 
array([[ 0.77552455,  0.89241629,         nan,  0.61187474],
       [ 0.62777982,  0.80245533,         nan,  0.66320306],
       [ 0.41578442,  0.26144272,  0.90260667,         nan],
       [ 0.65122428,  0.3211213 ,  0.81634856,         nan],
       [ 0.52957704,  0.73460363,  0.16484994,  0.20701344]])

In [158]: B
Out[158]: 
array([[ 0.55809925,  0.1339353 ,         nan,  0.35154039],
       [ 0.94484722,  0.23814073,  0.36048809,  0.20412318],
       [ 0.25191484,         nan,  0.43721322,  0.95810905],
       [ 0.69115038,  0.51490958,         nan,  0.44613473],
       [ 0.01709308,  0.81771896,  0.3229837 ,  0.64013882]])

In [159]: np.nansum(np.dstack((A,B)),2)
Out[159]: 
array([[ 1.3336238 ,  1.02635159,         nan,  0.96341512],
       [ 1.57262704,  1.04059606,  0.36048809,  0.86732624],
       [ 0.66769925,  0.26144272,  1.33981989,  0.95810905],
       [ 1.34237466,  0.83603089,  0.81634856,  0.44613473],
       [ 0.54667013,  1.55232259,  0.48783363,  0.84715226]])

Upvotes: 15

Related Questions