On Sun, Aug 28, 2011 at 3:03 PM, Bruce Ford <br...@clearscienceinc.com>wrote:
> Getting a strange result trying to divide two 3d arrays. I am getting
> a matrix of NaNs regardless of how I divide and I can't determine why.
>
> #opened a NetCDF file using python-netcdf4
>
> var1 = nc_file.variables['var1'] ###shape = [31,181,360] with a
> values ranging from 0 - 243 and NO NaNs in the array, dtype float32
> var2 = nc_file.variables['var2'] ###shape = [31,181,360] with a
> values ranging from 0 - 4 (mostly zeros) and NO NaNs in the array,
> dtype float32
>
> np.seterr(all='ignore') #in case problem has something do to with
> dividing by zero
>
> var1/var2 ###gives array of NaNs with shape of 31,181,360
>
> #doing the division one slice at a time doesn't help...
> for x in range(1,var1.shape[0]):
> var[x,:,:] = var1[x,:,:]/var2[x,:,:] ###gives array of
> NaNs with shape of 31,181,360
>
> var = np.divide(var1,var2) ###gives array of NaNs with shape of 31,181,360
>
>
> print "<p>where max: " + np.where(var1 == np.max(var1)) #prints
> (array([28]), array([79]), array([182]))
> print var1[28,79,182] #print 545
> print var2[28,79,182] ##prints 6
>
> #so there are values in this location that should not result in an
> NaN. Instead I get an entire array of NaNs
>
> What am I missing?
>
> Bruce
>
>
Just to be sure, what might appear to be all NaNs is not really. To find
out for sure, do this:
print np.any(np.isfinite(var1/var2))
If it is True, then the array is not full of NaNs and Infs, if False, then
that would be odd. When you print an array to the screen, it will often
only print part of the array.
Furthermore, the author of the post that you link to in your other email
misses some important facts about representation of numbers in computers.
The reason 0 / 0 (integers) did not return a NaN or 2 / 0 return an Inf is
that integers can not represent those values. Only floating point numbers
can. Furthermore, there is an important difference between Inf and NaN. It
is merely out of convenience that nan_to_num() would convert the Infs into
the largest (or smallest for -Inf) floating point number. Other functions
designed for NaN (such as nansum(), nanmax(), etc) do not treat Infs in this
manner.
Ben Root
------------------------------------------------------------------------------
Special Offer -- Download ArcSight Logger for FREE!
Finally, a world-class log management solution at an even better
price-free! And you'll get a free "Love Thy Logs" t-shirt when you
download Logger. Secure your free ArcSight Logger TODAY!
http://p.sf.net/sfu/arcsisghtdev2dev
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users