I am trying to avoid the warning RuntimeWarning: invalid value encountered in divide
in NumPy.
I thought I could do:
import numpy as np
A=np.array([0.0])
print A.dtype
with np.errstate(divide='ignore'):
B=A/A
print B
but this gives:
float64
./t.py:9: RuntimeWarning: invalid value encountered in divide
B=A/A
[ nan]
If I replace B=A/A
with np.float64(1.0) / 0.0
it gives no warning.
Solution: We can fix this Runtime Warning by using seterr method which takes invalid as a parameter and assign ignore as a value to it. By that, it can hide the warning message which contains invalid in that.
Why Is the Runtimewarning: Invalid Value Encountered in double_scalars Error Happening? The runtimewarning: invalid value encountered in double_scalars error happens when web developers try to perform certain mathematical operations that include numbers at the opposite end of the spectrum.
You need to set invalid
rather than divide
:
with np.errstate(invalid='ignore'):
^^^^^^^
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With