Numpy: Invalid Value Encountered in True_Divide

Numpy: Invalid Value Encountered in True_Divide

I have two numpy arrays and I am trying to divide one with the other and at the same time, I want to make sure that the entries where the divisor is 0, should just be replaced with 0.

So, I do something like:

log_norm_images = np.where(b_0 > 0, np.divide(diff_images, b_0), 0)

This gives me a run time warning of:

RuntimeWarning: invalid value encountered in true_divide

Now, I wanted to see what was going on and I did the following:

xx = np.isfinite(diff_images)
print (xx[xx == False])

xx = np.isfinite(b_0)
print (xx[xx == False])

However, both of these return empty arrays meaning that all the values in the arrays are finite. So, I am not sure where the invalid value is coming from. I am assuming checking b_0 > 0 in the np.where function takes care of the divide by 0.

The shape of the two arrays are (96, 96, 55, 64) and (96, 96, 55, 1)

8

4 Answers

You may have a NAN, INF, or NINF floating around somewhere. Try this:

np.isfinite(diff_images).all()
np.isfinite(b_0).all()

If one or both of those returns False, that's likely the cause of the runtime error.

4

The reason you get the runtime warning when running this:

log_norm_images = np.where(b_0 > 0, np.divide(diff_images, b_0), 0)

is that the inner expression

np.divide(diff_images, b_0)

gets evaluated first, and is run on all elements of diff_images and b_0 (even though you end up ignoring the elements that involve division-by-zero). In other words, the warning happens before the code that ignores those elements. That is why it's a warning and not an error: there are legitimate cases like this one where the division-by-zero is not a problem because it's being handled in a later operation.

Another useful Numpy command is nan_to_num(diff_images) By default it replaces in a Numpy array; NaN to zero, -INF to -(large number) and +INF to +(large number)

You can change the defaults, see

num = np.array([1,2,3,4,5])
den = np.array([1,1,0,1,1])
res = np.array([None]*5)
ix  = (den!=0)
res[ix] = np.divide( num[ix], den[ix] )
print(res)

[1.0 2.0 None 4.0 5.0]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Sarah Jenkins
Author

Sarah Jenkins

Sarah Jenkins is a veteran tech journalist with over 12 years of experience covering artificial intelligence, mobile innovations, and digital ethics. Her insights have appeared in leading technology publications worldwide.