Movatterモバイル変換


[0]ホーム

URL:


SciPy

numpy.errstate

classnumpy.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:
kwargs:{divide, over, under, invalid}

Keyword arguments. The valid keywords are the possible floating-pointexceptions. Each keyword should have a string value that defines thetreatment for the particular error. Possible values are{‘ignore’, ‘warn’, ‘raise’, ‘call’, ‘print’, ‘log’}.

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'}

Previous topic

numpy.geterrcall

Next topic

numpy.seterrobj

Quick search

  • © Copyright 2008-2018, The SciPy community.
  • Last updated on Jul 24, 2018.
  • Created usingSphinx 1.6.6.

[8]ページ先頭

©2009-2025 Movatter.jp