Post

PythonPython

r
ravi sankar

Posted on 19th April 2024|740 views

0
votes

Overflow Encountered In Exp

How to resolve overflow error in python?

 am using numpy.exp something like this

cc = np.array([ [0.130,0.14,-1334.1] ]) 

pIrint 1/(1+np.exp(-cc))

But this is showing an error like this

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

 

Answers
P
imran mohammad

Posted on 19th April 2024

Here is what you can try to resolve the error, use the decimal module that permits you to work with arbitrary precision floats. You can follow the below example where I am using a numpy array of floats with 100 digits

import numpy as np 

import decimal 

# use this precision 

decimal.getcontext().prec = 100 

# this is the Original array 

cc = np.array( [0.130,0.14,-1334.1] ) 

# this Fails 

print(1/(1 + np.exp(-cc))) 

# New array with the specified precision 

ccd = np.asarray([decimal.Decimal(el) for el in cc], dtype=object) 

# this Works!

print(1/(1 + np.exp(-ccd)))

Write your answer

STILL GOT QUERIES?

Get a Live FREE Demo
  • Explore the trending and niche courses and learning maps
  • Learn about tuition fee, payment plans, and scholarships
  • Get access to webinars and self-paced learning videos