[Numpy-discussion] Warning: invalid value encountered in subtract
Keith Goodman
kwgoodman@gmail....
Tue Nov 30 16:27:19 CST 2010
On Tue, Nov 30, 2010 at 2:25 PM, Robert Kern <robert.kern@gmail.com> wrote:
> On Tue, Nov 30, 2010 at 16:22, Keith Goodman <kwgoodman@gmail.com> wrote:
>> On Tue, Nov 30, 2010 at 1:41 PM, Skipper Seabold <jsseabold@gmail.com> wrote:
>>> On Tue, Nov 30, 2010 at 1:34 PM, Keith Goodman <kwgoodman@gmail.com> wrote:
>>>> After upgrading from numpy 1.4.1 to 1.5.1 I get warnings like
>>>> "Warning: invalid value encountered in subtract" when I run unit tests
>>>> (or timeit) using "python -c 'blah'" but not from an interactive
>>>> session. How can I tell the warnings to go away?
>>>
>>> If it's this type of floating point related stuff, you can use np.seterr
>>>
>>> In [1]: import numpy as np
>>>
>>> In [2]: np.log(1./np.array(0))
>>> Warning: divide by zero encountered in divide
>>> Out[2]: inf
>>>
>>> In [3]: orig_settings = np.seterr()
>>>
>>> In [4]: np.seterr(all="ignore")
>>> Out[4]: {'divide': 'print', 'invalid': 'print', 'over': 'print', 'under': 'ignor
>>> e'}
>>>
>>> In [5]: np.log(1./np.array(0))
>>> Out[5]: inf
>>>
>>> In [6]: np.seterr(**orig_settings)
>>> Out[6]: {'divide': 'ignore', 'invalid': 'ignore', 'over': 'ignore', 'under': 'ig
>>> nore'}
>>>
>>> In [7]: np.log(1./np.array(0))
>>> Warning: divide by zero encountered in divide
>>> Out[7]: inf
>>>
>>> I have been using the orig_settings so that I can take over the
>>> control of this from the user and then set it back to how it was.
>>
>> Thank, Skipper. That works. Do you wrap it in a try...except? And then
>> raise whatever brought you to the exception? Sounds like a pain.
>>
>> Is it considered OK for a package to change the state of np.seterr if
>> there is an error? Silly question. I'm just looking for an easy fix.
>
> with np.errstate(invalid='ignore'):
Ah! Thank you, Robert!
More information about the NumPy-Discussion
mailing list