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.)
答案 0 :(得分:2)
Try:
IntentService
or
np.seterr(invalid='ignore')
From the np.seterr(all='ignore')
docs:
seterr
- Invalid operation: result is not an expressible number, typically
indicates that a NaN was produced.
produces a 1/0
error/warning.
FloatingPointError: divide by zero encountered in true_divide
produces a 0/0
error/warning.
Warning: invalid value encountered in true_divide
will catch both.
all
can be used to temporarily change errstate
.
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
are normally issued the first time the problem arise in a run, and then are silent.