My attempt:
import numpy as np
np.seterr(divide='ignore')
a=np.array([4/3,0,0])
b=np.array([1,0,0])
np.divide(a,b)
The ouput I get:
__main__:1: RuntimeWarning: invalid value encountered in true_divide
array(1.33333333, nan, nan])
If I ask it again:
np.divide(a,b)
then no RuntimeWarning is displayed.
I'm happy with the array that is output, but I don't want the error message; that is what I thought seterr would fix. How can I get rid of the warning? (I would rather not write a for loop that makes exceptions for 0/0.)
Try:
np.seterr(invalid='ignore')
or
np.seterr(all='ignore')
From the seterr
docs:
- Invalid operation: result is not an expressible number, typically indicates that a NaN was produced.
1/0
produces a FloatingPointError: divide by zero encountered in true_divide
error/warning.
0/0
produces a Warning: invalid value encountered in true_divide
error/warning.
all
will catch both.
errstate
can be used to temporarily change np.seterr
.
In [1472]: with np.errstate(invalid='print'):
y=np.divide([1.2,0,0],[1,0,0])
......:
Warning: invalid value encountered in true_divide
warnings
are normally issued the first time the problem arise in a run, and then are silent.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments