numpy - Ignore divide by 0 warning in python -
i have function statistic issues:
import numpy np scipy.special import gamma gamma def foo(xdata): ... return x1 * ( ( #r numpy vector ( ((r - x2)/beta) ** (x3 -1) ) * ( np.exp( - ((r - x2) / x4) ) ) / ( x4 * gamma(x3)) ).real )
sometimes shell following warning:
runtimewarning: divide 0 encountered in...
i use numpy isinf
function correct results of function in other files need do. not need warning.
there way ignore message? in other words, not want shell print message.
i not want disable python warning, one.
you can disable warning numpy.seterr
. put before possible division zero:
np.seterr(divide='ignore')
that'll disable 0 division warnings globally. if want disable them little bit, can use numpy.errstate
in with
clause:
with np.errstate(divide='ignore'): # code here
for 0 zero division (undetermined, results in nan), error behaviour has changed numpy version 1.12.0: considered "invalid", while "divide".
thus, if there chance numerator 0 well, use
np.seterr(divide='ignore', invalid='ignore')
or
with np.errstate(divide='ignore', invalid='ignore'): # code here
see "compatibility" section in release notes, last paragraph before "new features" section:
comparing nan floating point numbers raises invalid runtime warning. if nan expected warning can ignored using np.errstate.
Comments
Post a Comment