This content originally appeared on CodeSource.io and was authored by Deven
In this article, you will learn how to solve runtimewarning: invalid value encountered in double_scalars.
Let’s look at a code example that produces the same error.
from numpy import cosh
print("3. ", 0.3+0.2-0.5)
print("4. ", 0.1+0.2-0.3)
x = 30
print("3. ", (1-cosh(x))/(1+cosh(x)))
x = 900
print("7. ", (1-cosh(x))/(1+cosh(x))) # this produces overflow error
Output of above code:
3. 0.0
4. 5.551115123125783e-17
3. -0.9999999999996257
<string>:8: RuntimeWarning: overflow encountered in cosh
<string>:8: RuntimeWarning: invalid value encountered in double_scalars
7. nan
>
The code snippet above is actually running into Divide by Zero Error. When the value or result is larger than the declared operation or data type in the program then this throws an overflow error indicating the value is exceeding the given or declared limit value.
how to solve runtimewarning: invalid value encountered in double_scalars.
you can use scipy.special.logsumexp
:
In [52]: from scipy.special import logsumexp
In [53]: res = np.exp(logsumexp(-3*d) - logsumexp(-3*e))
In [54]: res
Out[54]: 1.1050349147204485e-116
Alternatively, you can disable run time warnings (not recommended) like below:
import warnings
def f(x, y):
with warnings.catch_warnings():
warnings.simplefilter('ignore')
return ((x**(2))*y)/((x**(4)) + (y**(4)))
The post Solved – runtimewarning: invalid value encountered in double_scalars appeared first on CodeSource.io.
This content originally appeared on CodeSource.io and was authored by Deven
Deven | Sciencx (2021-03-08T12:24:16+00:00) Solved – runtimewarning: invalid value encountered in double_scalars. Retrieved from https://www.scien.cx/2021/03/08/solved-runtimewarning-invalid-value-encountered-in-double_scalars/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.