RuntimeWarning: overflow encountered in exp [Solved]

Borislav Hadzhiev

Last updated: Apr 11, 2024

Reading timeยท3 min

- RuntimeWarning: overflow encountered in exp [Solved]
- Resolving the warning when working with arrays
- Suppressing the NumPy warnings to resolve the issue

**The NumPy "RuntimeWarning: overflow encountered in exp" occurs when you try to
pass a larger number than is supported to the numpy.exp() method.**

**To solve the error, convert the number or array of numbers to np.float128
before calling exp().**

Here is an example of when the warning is shown.

main.py

`import numpy as np # โ๏ธ RuntimeWarning: overflow encountered in exp print(np.exp(800))`

The numpy.exp() method calculates the exponential of a number or all elements in an array.

However, the number we passed to the method is too large which caused the warning.

The largest representable NumPy float is 1.7976931348623157e+308 and its logarithm is ~709.782, so

`np.exp(800)`

returns `inf`

.One way to resolve the issue is to convert the number to `np.float128`

.

main.py

`import numpy as np num = 800 num = np.float128(num) # ๐๏ธ 2.7263745721125665673e+347 print(np.exp(num))`

The code for this article is available on GitHub

You will also get the warning when one or more of the elements in an array are
too large to be handled by the `np.exp()`

method.

main.py

`import numpy as np arr = np.array([1, 3, 800]) # โ๏ธ RuntimeWarning: overflow encountered in exp print(np.exp(arr))`

One way to solve the error is to set the
dtype keyword
argument to `np.float128`

to set the type of the numbers in the array to
`float128`

.

main.py

`import numpy as np arr = np.array([1, 3, 800], dtype=np.float128) # [2.71828183e+000 2.00855369e+001 2.72637457e+347] print(np.exp(arr))`

The code for this article is available on GitHub

We set the
dtype (data
type) keyword argument to `np.float128`

to cast the numbers in the array to
values of type `float128`

.

You can also use the `ndarray.astype()`

method to achieve the same result.

main.py

`import numpy as np arr = np.array([1, 3, 800]) arr = arr.astype(np.float128) # [2.71828183e+000 2.00855369e+001 2.72637457e+347] print(np.exp(arr))`

The numpy.ndarray.astype() method creates a copy of the array, cast to the specified type.

Alternatively, you can suppress the NumPy warning by using the `warnings`

built-in module.

main.py

`import warnings import numpy as np warnings.filterwarnings('ignore') arr = np.array([1, 3, 800]) # ๐๏ธ [ 2.71828183 20.08553692 inf] print(np.exp(arr))`

The code for this article is available on GitHub

The warnings.filterwarnings() method inserts an entry into the list of warning filters.

When the `action`

is set to `ignore`

, warnings are never printed.

However, if you use this approach you disable all warnings (not just ones from NumPy).

An alternative approach is to only disable RuntimeWarnings.

main.py

`import warnings import numpy as np warnings.filterwarnings('ignore', category=RuntimeWarning) arr = np.array([1, 3, 800]) # ๐๏ธ [ 2.71828183 20.08553692 inf] print(np.exp(arr))`

We set the `category`

keyword argument to `RuntimeWarning`

to only disable
RuntimeWarnings.

You can achieve the same result by only using the NumPy module.

main.py

`import numpy as np np.warnings.filterwarnings( 'ignore', category=RuntimeWarning ) arr = np.array([1, 3, 800]) print(np.exp(arr))`

The code for this article is available on GitHub

We used `numpy.warnings`

instead of the built-in `warnings`

module.

You can learn more about the related topics by checking out the following tutorials:

- ValueError: cannot reshape array of size X into shape Y
- lbfgs failed to converge (status=1): STOP: TOTAL NO. of ITERATIONS REACHED LIMIT
- How to draw empty circles on a Scatter Plot in Matplotlib
- TypeError: Image data cannot be converted to float [Solved]
- OverflowError: Python int too large to convert to C long
- How to use numpy.argsort in Descending order in Python
- Object arrays cannot be loaded when allow_pickle=False
- ufunc 'add' did not contain loop with signature matching types
- ValueError: Found array with dim 3. Estimator expected 2
- TypeError: ufunc 'isnan' not supported for the input types
- Columns have mixed types. Specify dtype option on import
- Convert a Row to a Column Header in a Pandas DataFrame
- Drop Unnamed: 0 columns from a Pandas DataFrame in Python