numpy.errstate(**kwargs)[source]¶Context manager for floating-point error handling.
Using an instance oferrstate as a context manager allows statements inthat context to execute with a known error handling behavior. Upon enteringthe context the error handling is set withseterr andseterrcall, andupon exiting it is reset to what it was before.
| Parameters: |
|
|---|
See also
Notes
Thewith statement was introduced in Python 2.5, and can only be usedthere by importing it:from__future__importwith_statement. Inearlier Python versions thewith statement is not available.
For complete documentation of the types of floating-point exceptions andtreatment options, seeseterr.
Examples
>>>from__future__importwith_statement# use 'with' in Python 2.5>>>olderr=np.seterr(all='ignore')# Set error handling to known state.
>>>np.arange(3)/0.array([ NaN, Inf, Inf])>>>withnp.errstate(divide='warn'):...np.arange(3)/0....__main__:2: RuntimeWarning: divide by zero encountered in dividearray([ NaN, Inf, Inf])
>>>np.sqrt(-1)nan>>>withnp.errstate(invalid='raise'):...np.sqrt(-1)Traceback (most recent call last): File"<stdin>", line2, in<module>FloatingPointError:invalid value encountered in sqrt
Outside the context the error handling behavior has not changed:
>>>np.geterr(){'over': 'warn', 'divide': 'warn', 'invalid': 'warn','under': 'ignore'}