[Numpy-discussion] Warning: invalid value encountered in subtract
Pierre GM
pgmdevlist@gmail....
Tue Nov 30 16:30:36 CST 2010
On Nov 30, 2010, at 11:22 PM, Keith Goodman 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.
I had to go through the try/except/set-and-reset-the-error-options dance myself in numpy.ma a few months ago. I realized that setting errors globally in a module (as was the case before) was a tad too sneaky. Sure, it was a bit of a pain, but at least you're not hiding anything....
More information about the NumPy-Discussion
mailing list