# [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.

Get notified of the best deals on our WordPress themes.

##### You May Also Like ## ModuleNotFoundError: No module named ‘flask_sqlalchemy’ ## [Solved] AttributeError: module ‘datetime’ has no attribute ‘utcnow’

Table of Contents Hide What is AttributeError: module ‘datetime’ has no attribute ‘utcnow’How to resolve AttributeError: module ‘datetime’ has no attribute ‘utcnow’Solution 1: Import the datetime module directly and access… ## ModuleNotFoundError: No module named ‘Cython’

Table of Contents Hide What is ModuleNotFoundError: No module named ‘Cython’?How to fix ModuleNotFoundError: No module named ‘Cython’?Solution 1 – Installing and using the Cython module in a proper waySolution… ## How to enable CORS on Django REST Framework?

If we are building an API layer using the Django REST framework and accessing these APIs in the front-end application we need to enable the CORS on Django Rest Framework… ## ModuleNotFoundError: No module named ‘pyautogui’

Table of Contents Hide What is ModuleNotFoundError: No module named ‘pyautogui’?How to fix ModuleNotFoundError: No module named ‘pyautogui’?Solution 1 – Installing and using the pyautogui module in a proper waySolution… ## TypeError: ‘module’ object is not callable

Python throws TypeError: ‘module’ object is not callable when you get confused between the class name and module name. There are several reasons why this might happen while coding. Let’s…