[Numpy-discussion] nan division warnings
Robert Kern
robert.kern at gmail.com
Tue Aug 30 23:47:58 EDT 2011
On Tue, Aug 30, 2011 at 22:39, mdekauwe <mdekauwe at gmail.com> wrote:
>
> Hi,
>
> this is probably my lack of understanding...when i set up some masks for 2
> arrays and try to divide one by the other I get a runtime warning. Seemingly
> this is when I am asking python to divide one nan by the other, however I
> thought by masking the array numpy would then know to ignore these nans? For
> example
>
> import numpy as np
> a = np.array([4.5, 6.7, 8.0, 9.0, 0.00001])
> b = np.array([0.0001, 6.7, 8.0, 9.0, 0.00001])
> a = np.ma.where(np.logical_or(a<0.01, b<0.01), np.nan, a)
> b = np.ma.where(np.logical_or(a<0.01, b<0.01), np.nan, b)
> a/b
>
> will produce
>
> …./numpy/ma/core.py:772: RuntimeWarning: invalid value encountered in
> absolute
> return umath.absolute(a) * self.tolerance >= umath.absolute(b)
>
> but of course give the correct result
>
> masked_array(data = [-- 1.0 1.0 1.0 --],
> mask = [ True False False False True],
> fill_value = 1e+20)
>
> But what is the correct way to do this array division such that I don't
> produce the warning?
Just don't put NaNs in.
[~]
|10> a = np.array([4.5, 6.7, 8.0, 9.0, 0.00001])
[~]
|11> b = np.array([0.0001, 6.7, 8.0, 9.0, 0.00001])
[~]
|12> mask = (a < 0.01) | (b < 0.01)
[~]
|13> ma = np.ma.masked_array(a, mask=mask)
[~]
|14> mb = np.ma.masked_array(b, mask=mask)
[~]
|15> ma / mb
masked_array(data = [-- 1.0 1.0 1.0 --],
mask = [ True False False False True],
fill_value = 1e+20)
--
Robert Kern
"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
-- Umberto Eco
More information about the NumPy-Discussion
mailing list