[Solved] RuntimeWarning: invalid value encountered in double_scalars

The RuntimeWarning: invalid value encountered in double_scalars mainly occurs when you perform a complex mathematical operation using NumPy that involves extremely small or very large numbers and also if we pass an invalid input such as NaN or null while performing NumPy operations.

In this tutorial, we will look at RuntimeWarning: invalid value encountered in double_scalars and how to resolve this error with examples.

What is RuntimeWarning: invalid value encountered in double_scalars?

We get the RuntimeWarning while performing the complex math operation with very large or extremely small numbers using NumPy.  

Few libraries in Python cannot handle such complex numbers and raise RuntimeWarning: invalid value encountered in double_scalars

Let us take a simple example to reproduce this error. In the below example, we have two NumPy arrays performing the Mathematical operations.

import numpy as np

# define two NumPy arrays
arr1 = np.array([[1000, 1100]])
arr2 = np.array([[1200, 3000]])

# Complex mathematical operation
result = np.exp(-3*arr1).sum() / np.exp(-3*arr1).sum()
print(result)

Output

RuntimeWarning: invalid value encountered in double_scalars

We receive a RuntimeWarning because the denominator is a complex number and extremely small that is closer to zero.

Hence when we perform the division with an extremely small denominator, we will get a large complex number as an output that Python cannot handle and raises a RuntimeWarning

Fix – RuntimeWarning: invalid value encountered in double_scalars

Since NumPy cannot handle large complex numbers, we can fix the error by using a special function logsumexp() from another library, SciPy.

The SciPy is a scientific computation library that uses NumPy underneath. SciPy stands for Scientific Python. The library is designed to handle such complex scientific scenarios.

The logsumexp() method can handle large and small complex numbers with exponents efficiently.

Let us modify our code to use the logsumexp() method from the SciPy library.

import numpy as np
from scipy.special import logsumexp

# define two NumPy arrays
arr1 = np.array([[1000, 1100]])
arr2 = np.array([[1200, 3000]])

# Complex mathematical operation
result = np.exp(logsumexp(-3 * arr1) - logsumexp(-3 * arr2))

print(result)

Output

3.7730203009299397e+260

Notice that the output is extremely large 3.7730203009299397e+260. Still, we did not receive any error because we have used the special logsumexp() method from the SciPy library that is designed to handle such scientific numbers.

Conclusion

The RuntimeWarning: invalid value encountered in double_scalars mainly occurs when you perform a complex mathematical operation that results in extremely large or small numbers and also if we pass an invalid input such as NaN or null while performing NumPy operations.

We can resolve the error by using a special method logsumexp() from the SciPy library that is designed to handle such scientific numbers and complex operations.