Reputation: 2502
How do you do a numpy dot product where the two vectors might have missing values? This seems to require many additional steps, is there an easier way to do this?:
v1 = np.array([1,4,2,np.nan,3])
v2 = np.array([np.nan,np.nan,2,4,1])
np.where(np.isnan(v1),0,v1).dot(np.where(np.isnan(v2),0,v2))
Upvotes: 6
Views: 13214
Reputation: 451
Another solution is to use masked arrays:
v1 = np.array([1,4,2,np.nan,3])
v2 = np.array([np.nan,np.nan,2,4,1])
v1_m = numpy.ma.array(v1, mask=numpy.isnan(v1))
v2_m = numpy.ma.array(v2, mask=numpy.isnan(v2))
numpy.ma.dot(v1_m, v2_m)
Upvotes: 2
Reputation: 221614
We can use np.nansum
to sum up the values ignoring NaNs
after element-wise multiplication -
np.nansum(v1*v2)
Sample run -
In [109]: v1
Out[109]: array([ 1., 4., 2., nan, 3.])
In [110]: v2
Out[110]: array([ nan, nan, 2., 4., 1.])
In [111]: np.where(np.isnan(v1),0,v1).dot(np.where(np.isnan(v2),0,v2))
Out[111]: 7.0
In [115]: v1*v2
Out[115]: array([ nan, nan, 4., nan, 3.])
In [116]: np.nansum(v1*v2)
Out[116]: 7.0
Upvotes: 8