# [Solved] RuntimeWarning: invalid value encountered in double_scalars

The RuntimeWarning: invalid value encountered in double_scalars 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.

## How to reproduce the error?

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 Exception.

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

## How to fix the error?

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 errors 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 which is designed to handle such scientific numbers and complex operations.

Subscribe to get notified of the latest articles. We will never spam you. Be a part of our ever-growing community.

##### You May Also Like ## ‘python’ is not recognized as an internal or external command, operable program or batch file.

The ‘python’ is not recognized as an internal or external command, operable program or batch file. error is encountered in the command prompt of Windows after you install Python on the windows… ## Pandas : How to Find Unique Values in a Column

We can find unique values of a column in Pandas DataFrame using the unique() function. The unique() method filters out only unique values from a dataframe column. In this tutorial,… If you use Selenium 4.0.0 or above and try to find the elements on the page using the find_element_by_*() method, you will get an error saying DeprecationWarning: find_element_by_* commands are deprecated. Please use… ## Python all()

Table of Contents Hide all() Syntaxall() Parametersall() Return ValueDifference between any() and all() functions in PythonExample 1 – Using all() function on Python ListsExample 2 – Using all() function on… ## [Solved] ImportError: No module named matplotlib.pyplot

Table of Contents Hide ImportError: No module named matplotlib.pyplotInstall Matplotlib in OSX/Linux Install Matplotlib in WindowsInstall Matplotlib in Anaconda The ImportError: No module named matplotlib.pyplot occurs if you have not installed… ## [Solved] AttributeError: ‘NoneType’ object has no attribute ‘get’

Table of Contents Hide What is AttributeError: ‘NoneType’ object has no attribute ‘get’?How to fix AttributeError: ‘NoneType’ object has no attribute ‘get’?Solution 1 – Call the get() method on valid…