[Numpy-discussion] Warning: invalid value encountered in subtract

Skipper Seabold jsseabold@gmail....
Tue Nov 30 15:41:20 CST 2010


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.

Skipper


More information about the NumPy-Discussion mailing list