Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)

like image 963
Luca Avatar asked Jan 08 '15 14:01

Luca


1 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.

like image 156
rchang Avatar answered Sep 20 '22 12:09

rchang