After division by 0, replace NaN with 0 in numpy arrays
This below should work and convert all NANs to 0
d[np.isnan(d)] = 0
If you want it all on one line, consider
d = np.nan_to_num(a1/a2)
Which will convert all NANs to 0, see here: http://docs.scipy.org/doc/numpy-1.10.0/reference/generated/numpy.nan_to_num.html
Note: When dividing by 0, you should follow @imp9's solution below to avoid unnecessary warnings or errors.
You should probably do the division in the context of np.errstate(divide='ignore', invalid='ignore')
so that division by 0 doesn't raise an error or warnings, whether the dividend itself is zero or not (the two are separate warnings).
with np.errstate(divide='ignore', invalid='ignore'):
d = a1/a2
#Geotob's solution
d[np.isnan(d)] = 0
If you want it to raise warnings the change 'ignore'
to 'warn'
. Reference