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?

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi Marcel, please expand your answer, the link might expire.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.