I have to sum two values obtained by np.average
as
for i in x :
a1 = np.average(function1(i))
a2 = np.average(function2(i))
plt.plot(i, a1+a2, 'o')
But the np.average
may return NaN
. Then, only points for which both a1
and a2
are available will be calculated.
How can I use zero instead of NaN
to make the sum for all points?
I tried to find a function in numpy to do so, but numpy.nan_to_num
is for arrays.
You can use numpy like this:
import numpy as np
a = [1, 2, np.nan]
a_sum = np.nansum(a)
a_mean = np.nanmean(a)
print('a = ', a) # [1, 2, nan]
print("a_sum = {}".format(a_sum)) # 3.0
print("a_mean = {}".format(a_mean)) # 1.5
You can also use :
clean_x = x[~np.isnan(x)]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With