0

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?

rafaelc
  • 57,686
  • 15
  • 58
  • 82
johnyboy325
  • 115
  • 9

1 Answers1

1

numpy.nanmean does this exactly. You can specify the axis, or leave it at None for a mean over the entire ndarray. Here's a link to the docs.

Marcel
  • 958
  • 1
  • 7
  • 18
  • Hi Marcel, please expand your answer, the link might expire. – bummi Apr 07 '19 at 16:27
  • Seems to work thanks, but I'll await other answers whether there is an even smoother way to do it. For now it also seems the fastest way, but who knows. – johnyboy325 Apr 08 '19 at 11:58