Solving the Problem – Overflow Error in Python’s numpy.exp function

Overflow Error in Python’s numpy.exp function is an error which occurs when you are dealing with very large numbers.

In this article I am going to help you solve this error and understand the root of the problem, also I am presenting other possible solutions that may work if the main solution does not work for you.

Exploring the Error : Overflow Error in Python’s numpy.exp function

This error, is an error which occurs when you are dealing with very large numbers.

The error message you have should look like the error message bellow. This is important because you do not want to mix between errors.

                                                                       #
/usr/local/lib/python2.7/site-packages/ipykernel/__main__.py:5: RuntimeWarning: overflow encountered in exp
                                                                       #

Bellow we will take care of the error using multiple possible solutions according to your needs.

Solution 1 : use numpy.clip.

This solution is for people who do not care a lot about precision. If precision is paramount to you, then skip this option.

You can use numpy.clip with float64 and float32.

Just like in the demonstration bellow.

                                                                       #
cc = np.clip(cc, -88.72, 88.72)   # with float32
cc = np.clip(cc, -709.78, 709.78) # with float64
                                                                       #

If this solution served you well, congratulations. If that is not the case try the solution bellow.

Solution 2 : use np.float128 instead of np.float64.

Usually, this problem occurs when you are dealing with very large numbers. The solution is to use np.float128 instead of np.float64, since the biggest number it can handle is exp(1234.1).

This is how you can use np.float128

                                                                       #
cc = np.array([[0.180,0.64,-1333.1]], dtype=np.float128)
                                                                       #

You can also use scipy.special.expit in order to suppress the warning.

                                                                       #
from scipy.special import expit
expit(cc)                                                              #

The error above was hard to deal with, I spent hours looking for a proper solution or set of solutions.

If the solutions above helped you, consider supporting us on Kofi, any help is appreciated.

Summing-up

I hope this article has been helpful and helped you solve this error. I hope you continue coding and learning Python, errors are part of the fun even when we hate them, cheers.

If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/