Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RuntimeWarning: overflow encountered in np.exp(x**2)

Tags:

python

numpy

exp

I need to calculate exp(x**2) where x = numpy.arange(30,90). This raises the warning:

RuntimeWarning: overflow encountered in exp
inf

I cannot safely ignore this warning, but neither SymPy nor mpmath is a solution and I need to perform array operations so a Numpy solution would be my dream.

Does anyone know how to handle this problem?

like image 561
AKG Avatar asked Jul 08 '14 17:07

AKG


2 Answers

You could use a data type that has the necessary range, for example decimal.Decimal:

>>> import numpy as np
>>> from decimal import Decimal
>>> x = np.arange(Decimal(30), Decimal(90))
>>> y = np.exp(x ** 2)
>>> y[-1]
Decimal('1.113246031563799750400684712E+3440')

But what are you using these numbers for? Could you avoid the exponentiation and work with logarithms? More detail about your problem would be helpful.

like image 75
Gareth Rees Avatar answered Nov 18 '22 20:11

Gareth Rees


I think you can use this method to solve this problem:

Normalized

I overcome the problem in this method. Before using this method, my classify accuracy is :86%. After using this method, my classify accuracy is :96%!!! It's great!
first:
Min-Max scaling
second:
Z-score standardization

These are common methods to implement normalization.
I use the first method. And I alter it. The maximum number is divided by 10. So the maximum number of the result is 10. Then exp(-10) will be not overflow!
I hope my answer will help you !(^_^)

like image 33
intoo Avatar answered Nov 18 '22 21:11

intoo