I have a numpy array where I will compute the LN difference between some numbers and then I will want to get the average of it. The problem is that the LN function will be undefinable with negative inputs, and division by zero can also occur. I would like to skip over these junk elements and I don't even want to include them in the array. What I want is to go through only the equations that can be computed and take their average.
I have tried to do this with numpy.nan which seems to be the most effective way to handle it, just put a numpy.nan for every index value that can't be computed. More simply I just initialize the array as nan from the get go and then just fill up the computable elements and by default leave everything else nan.
Like this:
LN_ARRAY = numpy.full(array_size, numpy.nan, dtype=float, order='C')
...
for i in range(7,array_size):
if(F>0.0): LN_ARRAY[i]=abs( math.log( A / F ) )
# make sure F can't be zero or negative, A is by default always non zero positive
Now the problem is that if I just take the average of this with numpy.average(LN_ARRAY)
it will just output nan
because I will always have nan
values since I go from the 8th element to fill up the array, plus the additional nan
due to the if test.
Is there a way to compute the average of this array by excluding the nan
or do it in a more efficient way alltogether?