简体   繁体   English

运行时警告:在np.exp中遇到溢出(x ** 2)

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

I need to calculate exp(x**2) where x = numpy.arange(30,90) . 我需要计算exp(x**2) ,其中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. 我无法安全地忽略这个警告,但SymPy和mpmath都不是解决方案,我需要执行数组操作,因此Numpy解决方案将成为我的梦想。

Does anyone know how to handle this problem? 有谁知道如何处理这个问题?

You could use a data type that has the necessary range, for example decimal.Decimal : 您可以使用具有必要范围的数据类型,例如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. 有关您的问题的更多细节将是有帮助的。

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%. 在使用此方法之前,我的分类准确度为:86%。 After using this method, my classify accuracy is :96%!!! 使用此方法后,我的分类准确度为:96%! It's great! 这很棒!
first: 第一:
Min-Max scaling Min-Max缩放
second: 第二:
Z-score standardization Z分数标准化

These are common methods to implement normalization . 这些是实现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 ! 最大数除以10.因此结果的最大数为10.然后exp(-10)将不会overflow
I hope my answer will help you !(^_^) 我希望我的回答能帮到你!(^_^)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM