One warning you may encounter in Python is:
RuntimeWarning: overflow encountered in exp
This warning occurs when you use the NumPy exp function, but use a value that is too large for it to handle.
It’s important to note that this is simply a warning and that NumPy will still carry out the calculation you requested, but it provides the warning by default.
When you encounter this warning, you have two options:
1. Ignore it.
2. Suppress the warning entirely.
The following example shows how to address this warning in practice.
How to Reproduce the Warning
Suppose we perform the following calculation in Python:
import numpy as np #perform some calculation print(1/(1+np.exp(1140))) 0.0 /srv/conda/envs/notebook/lib/python3.7/site-packages/ipykernel_launcher.py:3: RuntimeWarning: overflow encountered in exp
Notice that NumPy performs the calculation (the result is 0.0) but it still prints the RuntimeWarning.
This warning is printed because the value np.exp(1140) represents e1140, which is a massive number.
We basically requested NumPy to perform the following calculation:
- 1 / (1 + massive number)
This can be reduced to:
- 1 / massive number
This is effectively 0, which is why NumPy calculated the result to be 0.0.
How to Suppress the Warning
If we’d like, we can use the warnings package to suppress warnings as follows:
import numpy as np import warnings #suppress warnings warnings.filterwarnings('ignore') #perform some calculation print(1/(1+np.exp(1140))) 0.0
Notice that NumPy performs the calculation and does not display a RuntimeWarning.
Note: In general, warnings can be helpful for identifying bits of code that take a long time to run so be highly selective when deciding to suppress warnings.
Additional Resources
The following tutorials explain how to fix other common errors in Python:
How to Fix KeyError in Pandas
How to Fix: ValueError: cannot convert float NaN to integer
How to Fix: ValueError: operands could not be broadcast together with shapes
In this article we will discuss how to fix RuntimeWarning: overflow encountered in exp in Python.
This warning occurs while using the NumPy library’s exp() function upon using on a value that is too large. This function is used to calculate the exponential of all elements in the input array or an element (0-D Array of NumPy).
Example: Code to depict warning
Python3
import numpy as np
print(np.exp(789))
Output:

The output is infinity cause e^789 is a very large value
This warning occurs because the maximum size of data that can be used in NumPy is float64 whose maximum range is 1.7976931348623157e+308. Upon taking logarithm its value becomes 709.782. For any larger value than this, the warning is generated.
Let us discuss ways to fix this.
Method 1: Using float128
The data type float64 can be changed to float128.
Example: Program to fix the warning
Python3
import numpy as np
x = 789
x = np.float128(x)
print(np.exp(x))
Output:

Using float128
For ndarray you can use the dtype parameter of the array method.
Example: Program to produce output without using dtype
Python3
import numpy as np
cc = np.array([789, 0.34, -1234.1])
print(np.exp(cc))
Output:

without using dtype
Example: Fixed warning by using dtype
Python3
import numpy as np
cc = np.array([789, 0.34, -1234.1], dtype=np.float128)
print(np.exp(cc))
Output:

using dtype
Method 2: Using filterwarnings()
Warning messages are typically issued in situations where it is useful to alert the user of some condition in a program, where that condition (normally) doesn’t warrant raising an exception and terminating the program. To deal with warnings there is a built-in module called warning. To read more about python warnings you can check out this article.
The filterwarnings() function can be used to control the behavior of warnings in your programs. The warnings filter controls whether warnings are ignored, displayed, or turned into errors (raising an exception). This can be done using different actions:
- “ignore” to never print matching warnings
- “error” to turn matching warnings into exceptions
- “once” to print only the first occurrence of matching warnings, regardless of location
Syntax:
warnings.filterwarnings(action, message=”, category=Warning, module=”, lineno=0, append=False)
Example: Fixing warning using filterwarnings()
Python3
import numpy as np
import warnings
warnings.filterwarnings('ignore')
x = 789
x = np.float128(x)
print(np.exp(x))
Output:

using filterwarnings()
17 авг. 2022 г.
читать 1 мин
Одно предупреждение, с которым вы можете столкнуться в Python:
RuntimeWarning: overflow encountered in exp
Это предупреждение появляется, когда вы используете функцию выражения NumPy , но используете слишком большое значение для ее обработки.
Важно отметить, что это просто предупреждение и что NumPy все равно выполнит запрошенный вами расчет, но по умолчанию выдает предупреждение.
Когда вы сталкиваетесь с этим предупреждением, у вас есть два варианта:
1. Не обращайте внимания.
2. Полностью отключите предупреждение.
В следующем примере показано, как устранить это предупреждение на практике.
Как воспроизвести предупреждение
Предположим, мы выполняем следующий расчет в Python:
import numpy as np
#perform some calculation
print(1/(1+np.exp (1140)))
0.0
/srv/conda/envs/notebook/lib/python3.7/site-packages/ipykernel_launcher.py:3:
RuntimeWarning: overflow encountered in exp
Обратите внимание, что NumPy выполняет вычисления (результат равен 0,0), но по-прежнему печатает RuntimeWarning .
Это предупреждение выводится, потому что значение np.exp(1140) представляет e 1140 , что является массивным числом.
В основном мы просили NumPy выполнить следующие вычисления:
- 1 / (1 + массивное число)
Это можно сократить до:
- 1 / массивное число
Фактически это 0, поэтому NumPy вычислил результат равным 0.0 .
Как подавить предупреждение
Если мы хотим, мы можем использовать пакет warnings для подавления предупреждений следующим образом:
import numpy as np
import warnings
#suppress warnings
warnings. filterwarnings('ignore')
#perform some calculation
print(1/(1+np.exp (1140)))
0.0
Обратите внимание, что NumPy выполняет вычисления и не отображает RuntimeWarning.
Примечание.Как правило, предупреждения могут быть полезны для определения фрагментов кода, выполнение которых занимает много времени, поэтому будьте очень избирательны при принятии решения об отключении предупреждений.
Дополнительные ресурсы
В следующих руководствах объясняется, как исправить другие распространенные ошибки в Python:
Как исправить KeyError в Pandas
Как исправить: ValueError: невозможно преобразовать число с плавающей запятой NaN в целое число
Как исправить: ValueError: операнды не могли транслироваться вместе с фигурами
The NumPy is a Python package that is rich with utilities for playing around with large multi-dimensional matrices and arrays and performing both complex and straightforward mathematical operations over them.
These utilities are dynamic to the inputs and highly optimized and fast. The NumPy package has a function exp() that calculates the exponential of all the elements of an input numpy array.
In other words, it computes ex, x is every number of the input numpy array, and e is the Euler’s number that is approximately equal to 2.71828.
Since this calculation can result in a huge number, some data types fail to handle such big values, and hence, this function will return inf and an error instead of a valid floating value.
For example, this function will return 8.21840746e+307 for numpy.exp(709) but runtimeWarning: overflow encountered in exp inf for numpy.exp(710).
In this article, we will learn how to fix this issue.
Fix for Overflow in numpy.exp() Function in Python NumPy
We have to store values in a data type capable of holding such large values to fix this issue.
For example, np.float128 can hold way bigger numbers than float64 and float32. All we have to do is just typecast each value of an array to a bigger data type and store it in a numpy array.
The following Python code depicts this.
import numpy as np
a = np.array([1223, 2563, 3266, 709, 710], dtype = np.float128)
print(np.exp(a))
Output:
[1.38723925e+0531 1.24956001e+1113 2.54552810e+1418 8.21840746e+0307
2.23399477e+0308]
Although the above Python code runs seamlessly without any issues, still, we are prone to the same error.
The reason behind it is pretty simple; even np.float128 has a threshold value for numbers it can hold. Every data type has an upper-cap, and if that upper-cap is crossed, things start getting buggy, and programs start running into overflow errors.
To understand the point mentioned above, refer to the following Python code. Even though np.float128 solved our problem in the last Python code snippet, it would not work for even bigger values.
import numpy as np
a = np.array([1223324, 25636563, 32342266, 235350239, 27516346320], dtype = np.float128)
print(np.exp(a))
Output:
<string>:4: RuntimeWarning: overflow encountered in exp
[inf inf inf inf inf]
The exp() function returns an infinity for every value in the numpy array.
To learn about the
numpy.exp()function, refer to the officialNumPydocumentation here.
In this article we will discuss how to fix RuntimeWarning: overflow encountered in exp in Python.
This warning occurs while using the NumPy library’s exp() function upon using on a value that is too large. This function is used to calculate the exponential of all elements in the input array or an element (0-D Array of NumPy).
Example: Code to depict warning
Python3
import numpy as np
print(np.exp(789))
Output:
The output is infinity cause e^789 is a very large value
This warning occurs because the maximum size of data that can be used in NumPy is float64 whose maximum range is 1.7976931348623157e+308. Upon taking logarithm its value becomes 709.782. For any larger value than this, the warning is generated.
Let us discuss ways to fix this.
Method 1: Using float128
The data type float64 can be changed to float128.
Example: Program to fix the warning
Python3
import numpy as np
x = 789
x = np.float128(x)
print(np.exp(x))
Output:
For ndarray you can use the dtype parameter of the array method.
Example: Program to produce output without using dtype
Python3
import numpy as np
cc = np.array([789, 0.34, -1234.1])
print(np.exp(cc))
Output:
Example: Fixed warning by using dtype
Python3
import numpy as np
cc = np.array([789, 0.34, -1234.1], dtype=np.float128)
print(np.exp(cc))
Output:
Method 2: Using filterwarnings()
Warning messages are typically issued in situations where it is useful to alert the user of some condition in a program, where that condition (normally) doesn’t warrant raising an exception and terminating the program. To deal with warnings there is a built-in module called warning. To read more about python warnings you can check out this article.
The filterwarnings() function can be used to control the behavior of warnings in your programs. The warnings filter controls whether warnings are ignored, displayed, or turned into errors (raising an exception). This can be done using different actions:
- “ignore” to never print matching warnings
- “error” to turn matching warnings into exceptions
- “once” to print only the first occurrence of matching warnings, regardless of location
Syntax:
warnings.filterwarnings(action, message=”, category=Warning, module=”, lineno=0, append=False)
Example: Fixing warning using filterwarnings()
Python3
import numpy as np
import warnings
warnings.filterwarnings('ignore')
x = 789
x = np.float128(x)
print(np.exp(x))
Output:
In this article we will discuss how to fix RuntimeWarning: overflow encountered in exp in Python.
This warning occurs while using the NumPy library’s exp() function upon using on a value that is too large. This function is used to calculate the exponential of all elements in the input array or an element (0-D Array of NumPy).
Example: Code to depict warning
Python3
import numpy as np
print(np.exp(789))
Output:
The output is infinity cause e^789 is a very large value
This warning occurs because the maximum size of data that can be used in NumPy is float64 whose maximum range is 1.7976931348623157e+308. Upon taking logarithm its value becomes 709.782. For any larger value than this, the warning is generated.
Let us discuss ways to fix this.
Method 1: Using float128
The data type float64 can be changed to float128.
Example: Program to fix the warning
Python3
import numpy as np
x = 789
x = np.float128(x)
print(np.exp(x))
Output:
For ndarray you can use the dtype parameter of the array method.
Example: Program to produce output without using dtype
Python3
import numpy as np
cc = np.array([789, 0.34, -1234.1])
print(np.exp(cc))
Output:
Example: Fixed warning by using dtype
Python3
import numpy as np
cc = np.array([789, 0.34, -1234.1], dtype=np.float128)
print(np.exp(cc))
Output:
Method 2: Using filterwarnings()
Warning messages are typically issued in situations where it is useful to alert the user of some condition in a program, where that condition (normally) doesn’t warrant raising an exception and terminating the program. To deal with warnings there is a built-in module called warning. To read more about python warnings you can check out this article.
The filterwarnings() function can be used to control the behavior of warnings in your programs. The warnings filter controls whether warnings are ignored, displayed, or turned into errors (raising an exception). This can be done using different actions:
- “ignore” to never print matching warnings
- “error” to turn matching warnings into exceptions
- “once” to print only the first occurrence of matching warnings, regardless of location
Syntax:
warnings.filterwarnings(action, message=”, category=Warning, module=”, lineno=0, append=False)
Example: Fixing warning using filterwarnings()
Python3
import numpy as np
import warnings
warnings.filterwarnings('ignore')
x = 789
x = np.float128(x)
print(np.exp(x))
Output:
Ok I’m using last version (master) of statsmodels and scipy 0.18.1.
Poisson with const + pp works IF I modify DiscreteResults.llnull.
Now, before adding all the variables I have, we move to the Negative Binomial:
I add
model = sm.NegativeBinomial(y, X_sm, )
start = res.params.append(pd.Series(0.9, index=['alpha']))
res = model.fit(maxiter=2000, start_params=start, method="minimize")
And the result is that the llnullmodel does not fit (even with the method=’nm’)
Optimization terminated successfully.
Current function value: 884.916016
Iterations: 11
Function evaluations: 12
Gradient evaluations: 11
Hessian evaluations: 10
Optimization terminated successfully.
Current function value: 6.452032
Iterations: 12
Function evaluations: 13
Gradient evaluations: 13
/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/statsmodels-0.8.0-py3.4-macosx-10.6-intel.egg/statsmodels/discrete/discrete_model.py:901: RuntimeWarning: overflow encountered in exp
return stats.poisson.cdf(y, np.exp(X))
/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/statsmodels-0.8.0-py3.4-macosx-10.6-intel.egg/statsmodels/discrete/discrete_model.py:1172: RuntimeWarning: overflow encountered in exp
L = np.exp(np.dot(X,params) + exposure + offset)
/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/statsmodels-0.8.0-py3.4-macosx-10.6-intel.egg/statsmodels/discrete/discrete_model.py:1112: RuntimeWarning: overflow encountered in exp
L = np.exp(np.dot(X,params) + offset + exposure)
/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/scipy/stats/_discrete_distns.py:451: RuntimeWarning: invalid value encountered in greater_equal
return mu >= 0
NegativeBinomial Regression Results
==============================================================================
Dep. Variable: y No. Observations: 270
Model: NegativeBinomial Df Residuals: 268
Method: MLE Df Model: 1
Date: Sat, 04 Mar 2017 Pseudo R-squ.: nan
Time: 16:22:04 Log-Likelihood: -1742.0
converged: True LL-Null: nan
LLR p-value: nan
==============================================================================
coef std err z P>|z| [0.025 0.975]
------------------------------------------------------------------------------
const 6.9013 0.145 47.616 0.000 6.617 7.185
pp 0.7432 0.181 4.111 0.000 0.389 1.098
alpha 5.6705 0.452 12.534 0.000 4.784 6.557
==============================================================================
/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/statsmodels-0.8.0-py3.4-macosx-10.6-intel.egg/statsmodels/base/model.py:481: HessianInversionWarning: Inverting hessian failed, no bse or cov_params available
'available', HessianInversionWarning)
I naively try also to put method=»minimize», min_method=’dogleg’ to fit llnull in DiscreteResults and I have:
ValueError Traceback (most recent call last)
<ipython-input-2-d821dbf330a6> in <module>()
44
45
---> 46 print(res.summary())
/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/statsmodels-0.8.0-py3.4-macosx-10.6-intel.egg/statsmodels/discrete/discrete_model.py in summary(self, yname, xname, title, alpha, yname_list)
2530 ('Df Residuals:', None),
2531 ('Df Model:', None),
-> 2532 ('Pseudo R-squ.:', ["%#6.4g" % self.prsquared]),
2533 ('Log-Likelihood:', None),
2534 ('LL-Null:', ["%#8.5g" % self.llnull]),
/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/statsmodels-0.8.0-py3.4-macosx-10.6-intel.egg/statsmodels/tools/decorators.py in __get__(self, obj, type)
95 if _cachedval is None:
96 # Call the "fget" function
---> 97 _cachedval = self.fget(obj)
98 # Set the attribute in obj
99 # print("Setting %s in cache to %s" % (name, _cachedval))
/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/statsmodels-0.8.0-py3.4-macosx-10.6-intel.egg/statsmodels/discrete/discrete_model.py in prsquared(self)
2380 @cache_readonly
2381 def prsquared(self):
-> 2382 return 1 - self.llf/self.llnull
2383
2384 @cache_readonly
/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/statsmodels-0.8.0-py3.4-macosx-10.6-intel.egg/statsmodels/tools/decorators.py in __get__(self, obj, type)
95 if _cachedval is None:
96 # Call the "fget" function
---> 97 _cachedval = self.fget(obj)
98 # Set the attribute in obj
99 # print("Setting %s in cache to %s" % (name, _cachedval))
/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/statsmodels-0.8.0-py3.4-macosx-10.6-intel.egg/statsmodels/discrete/discrete_model.py in llnull(self)
2401 # TestPoissonConstrained1a.test_smoke
2402 res_null = mod_null.fit(disp=0, warn_convergence=False,
-> 2403 maxiter=10000, method="minimize", min_method='dogleg')
2404 return res_null.llf
2405
/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/statsmodels-0.8.0-py3.4-macosx-10.6-intel.egg/statsmodels/discrete/discrete_model.py in fit(self, start_params, method, maxiter, full_output, disp, callback, cov_type, cov_kwds, use_t, **kwargs)
2267 maxiter=maxiter, method=method, disp=disp,
2268 full_output=full_output, callback=lambda x:x,
-> 2269 **kwargs)
2270 # TODO: Fix NBin _check_perfect_pred
2271 if self.loglike_method.startswith('nb'):
/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/statsmodels-0.8.0-py3.4-macosx-10.6-intel.egg/statsmodels/discrete/discrete_model.py in fit(self, start_params, method, maxiter, full_output, disp, callback, **kwargs)
821 cntfit = super(CountModel, self).fit(start_params=start_params,
822 method=method, maxiter=maxiter, full_output=full_output,
--> 823 disp=disp, callback=callback, **kwargs)
824 discretefit = CountResults(self, cntfit)
825 return CountResultsWrapper(discretefit)
/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/statsmodels-0.8.0-py3.4-macosx-10.6-intel.egg/statsmodels/discrete/discrete_model.py in fit(self, start_params, method, maxiter, full_output, disp, callback, **kwargs)
202 mlefit = super(DiscreteModel, self).fit(start_params=start_params,
203 method=method, maxiter=maxiter, full_output=full_output,
--> 204 disp=disp, callback=callback, **kwargs)
205
206 return mlefit # up to subclasses to wrap results
/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/statsmodels-0.8.0-py3.4-macosx-10.6-intel.egg/statsmodels/base/model.py in fit(self, start_params, method, maxiter, full_output, disp, fargs, callback, retall, skip_hessian, **kwargs)
457 callback=callback,
458 retall=retall,
--> 459 full_output=full_output)
460
461 #NOTE: this is for fit_regularized and should be generalized
/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/statsmodels-0.8.0-py3.4-macosx-10.6-intel.egg/statsmodels/base/optimizer.py in _fit(self, objective, gradient, start_params, fargs, kwargs, hessian, method, maxiter, full_output, disp, callback, retall)
191 disp=disp, maxiter=maxiter, callback=callback,
192 retall=retall, full_output=full_output,
--> 193 hess=hessian)
194
195 optim_settings = {'optimizer': method, 'start_params': start_params,
/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/statsmodels-0.8.0-py3.4-macosx-10.6-intel.egg/statsmodels/base/optimizer.py in _fit_minimize(f, score, start_params, fargs, kwargs, disp, maxiter, callback, retall, full_output, hess)
246
247 res = optimize.minimize(f, start_params, args=fargs, method=kwargs['min_method'],
--> 248 jac=score, hess=hess, callback=callback, options=options)
249
250 xopt = res.x
/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/scipy/optimize/_minimize.py in minimize(fun, x0, args, method, jac, hess, hessp, bounds, constraints, tol, callback, options)
459 elif meth == 'dogleg':
460 return _minimize_dogleg(fun, x0, args, jac, hess,
--> 461 callback=callback, **options)
462 elif meth == 'trust-ncg':
463 return _minimize_trust_ncg(fun, x0, args, jac, hess, hessp,
/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/scipy/optimize/_trustregion_dogleg.py in _minimize_dogleg(fun, x0, args, jac, hess, **trust_region_options)
35 return _minimize_trust_region(fun, x0, args=args, jac=jac, hess=hess,
36 subproblem=DoglegSubproblem,
---> 37 **trust_region_options)
38
39
/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/scipy/optimize/_trustregion.py in _minimize_trust_region(fun, x0, args, jac, hess, hessp, subproblem, initial_trust_radius, max_trust_radius, eta, gtol, maxiter, disp, return_all, callback, **unknown_options)
172 # has reached the trust region boundary or not.
173 try:
--> 174 p, hits_boundary = m.solve(trust_radius)
175 except np.linalg.linalg.LinAlgError as e:
176 warnflag = 3
/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/scipy/optimize/_trustregion_dogleg.py in solve(self, trust_radius)
96 # This is the optimum for the quadratic model function.
97 # If it is inside the trust radius then return this point.
---> 98 p_best = self.newton_point()
99 if scipy.linalg.norm(p_best) < trust_radius:
100 hits_boundary = False
/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/scipy/optimize/_trustregion_dogleg.py in newton_point(self)
58 g = self.jac
59 B = self.hess
---> 60 cho_info = scipy.linalg.cho_factor(B)
61 self._newton_point = -scipy.linalg.cho_solve(cho_info, g)
62 return self._newton_point
/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/scipy/linalg/decomp_cholesky.py in cho_factor(a, lower, overwrite_a, check_finite)
130 """
131 c, lower = _cholesky(a, lower=lower, overwrite_a=overwrite_a, clean=False,
--> 132 check_finite=check_finite)
133 return c, lower
134
/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/scipy/linalg/decomp_cholesky.py in _cholesky(a, lower, overwrite_a, clean, check_finite)
18
19 if check_finite:
---> 20 a1 = asarray_chkfinite(a)
21 else:
22 a1 = asarray(a)
/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/numpy/lib/function_base.py in asarray_chkfinite(a, dtype, order)
1213 if a.dtype.char in typecodes['AllFloat'] and not np.isfinite(a).all():
1214 raise ValueError(
-> 1215 "array must not contain infs or NaNs")
1216 return a
1217
ValueError: array must not contain infs or NaNs
Я хочу использовать numpy.exp так:
cc = np.array([
[0.120,0.34,-1234.1]
])
print 1/(1+np.exp(-cc))
Но это дает мне ошибку:
/usr/local/lib/python2.7/site-packages/ipykernel/__main__.py:5: RuntimeWarning: overflow encountered in exp
Не могу понять почему? Как я могу это исправить? Кажется, проблема в третьем номере (-1234.1)
4 ответа
Лучший ответ
Как говорит Фугледе, проблема в том, что np.float64 не может обработать такое большое число, как exp(1234.1). Попробуйте использовать np.float128 вместо этого:
>>> cc = np.array([[0.120,0.34,-1234.1]], dtype=np.float128)
>>> cc
array([[ 0.12, 0.34, -1234.1]], dtype=float128)
>>> 1 / (1 + np.exp(-cc))
array([[ 0.52996405, 0.58419052, 1.0893812e-536]], dtype=float128)
Обратите внимание, что существуют определенные причуды с использованием расширенной точности. Это может не работать в Windows; вы на самом деле не получаете полных 128 бит точности; и вы можете потерять точность всякий раз, когда число проходит через чистый питон. Подробнее о здесь можно прочитать здесь.
Для большинства практических целей вы, вероятно, можете приблизить 1 / (1 + <a large number>) к нулю. То есть просто игнорируйте предупреждение и двигайтесь дальше. Numpy позаботится о приближении для вас (при использовании np.float64):
>>> 1 / (1 + np.exp(-cc))
/usr/local/bin/ipython3:1: RuntimeWarning: overflow encountered in exp
#!/usr/local/bin/python3.4
array([[ 0.52996405, 0.58419052, 0. ]])
Если вы хотите отключить предупреждение, вы можете использовать {{ X0}}, как предложил WarrenWeckesser в комментарии к вопросу:
>>> from scipy.special import expit
>>> expit(cc)
array([[ 0.52996405, 0.58419052, 0. ]])
12
Praveen
19 Фев 2018 в 23:13
Exp (-1234.1) слишком мала для 32-битных или 64-битных чисел с плавающей точкой. Поскольку он не может быть представлен, numpy выдает правильное предупреждение.
Используя числа IEEE 754 32bit floating-point, наименьшее положительное число, которое он может представить, это 2^(-149), что примерно равно 1e-45.
Если вы используете IEEE 754 64 bit floating-point числа, наименьшее положительное число — 2^(-1074), которое является грубым 1e-327.
В любом случае, он не может представлять собой такое же маленькое число, как exp (-1234.1), которое составляет около 1e-535.
Вы должны использовать функцию expit из scipy для вычисления сигмовидной функции. Это даст вам лучшую точность.
Для практических целей exp (-1234.1) — очень небольшое число. Если в вашем случае имеет смысл округление до нуля, numpy дает доброкачественные результаты, округляя его до нуля.
0
user1559897
9 Дек 2019 в 15:17
Наибольшее значение, представляемое с плавающей точкой numpy, равно 1.7976931348623157e + 308, логарифм которого равен примерно 709.782, поэтому невозможно представить np.exp(1234.1).
In [1]: import numpy as np
In [2]: np.finfo('d').max
Out[2]: 1.7976931348623157e+308
In [3]: np.log(_)
Out[3]: 709.78271289338397
In [4]: np.exp(709)
Out[4]: 8.2184074615549724e+307
In [5]: np.exp(710)
/usr/local/bin/ipython:1: RuntimeWarning: overflow encountered in exp
#!/usr/local/bin/python3.5
Out[5]: inf
6
fuglede
21 Ноя 2016 в 18:07
Возможное решение — использовать модуль decimal, который позволяет работать с произвольными значениями точности. Вот пример, где используется массив numpy чисел с точностью до 100 цифр:
import numpy as np
import decimal
# Precision to use
decimal.getcontext().prec = 100
# Original array
cc = np.array(
[0.120,0.34,-1234.1]
)
# 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)
# Works!
print(1/(1 + np.exp(-ccd)))
2
jmd_dk
21 Ноя 2016 в 19:06
