社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

RuntimeWarning:在cosh--Python中遇到溢出。这是什么意思?

Dimitri_896 • 4 年前 • 2003 次点击  

我正在进行以下计算:

N = 2**15
dx = 0.1
x = np.arange(-N/2,N/2)
u0 = np.zeros([N, 1])
L = N * dx
x0 = x[1] + 2 * delta
delta = 15

while x0 < L - delta:

    l1 = 1.267;
    x0 = x0 + delta
    r = 1/(l1*np.cosh(x)**2)
    u0 = r + u0

基本上,x0<L-delta这个循环将运行2^15分。

这可以很好地翻译成MATLAB,但python给了我以下错误:

RuntimeWarning:在cosh中遇到溢出
r=1/(l1*np.cosh(x)**2)

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/132981
文章 [ 1 ]  |  最新文章 4 年前
hpaulj
Reply   •   1 楼
hpaulj    4 年前

cosh 长得很大:

In [70]: np.cosh(2**10)
<ipython-input-70-c4511154ec1e>:1: RuntimeWarning: overflow encountered in cosh
  np.cosh(2**10)
Out[70]: inf

为什么你的 x 这么宽?在大部分范围内,这个的反比 科什 将是0。

In [72]: N=2**15; x = np.arange(-N/2,N/2)
In [73]: len(x)
Out[73]: 32768
In [74]: r = 1/(np.cosh(x)**2)
<ipython-input-74-404fbe3be390>:1: RuntimeWarning: overflow encountered in cosh
  r = 1/(np.cosh(x)**2)
<ipython-input-74-404fbe3be390>:1: RuntimeWarning: overflow encountered in square
  r = 1/(np.cosh(x)**2)
In [75]: r[:10]
Out[75]: array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])

In [77]: np.sum(r<1e-16)
Out[77]: 32729

看看+-20的范围 十、

In [88]: x = np.arange(-20,20)
In [89]: r = 1/(np.cosh(x)**2)
In [90]: r
Out[90]: 
array([1.69934170e-17, 1.25565312e-16, 9.27809132e-16, 6.85563373e-15,
       5.06566622e-14, 3.74304919e-13, 2.76576004e-12, 2.04363561e-11,
      ...
       1.00000000e+00, 4.19974342e-01, 7.06508249e-02, 9.86603717e-03,
       ...
       1.51005382e-10, 2.04363561e-11, 2.76576004e-12, 3.74304919e-13,
       5.06566622e-14, 6.85563373e-15, 9.27809132e-16, 1.25565312e-16])

压制警告

In [148]: x=np.array([-2**15,-2**4,0,2**4,2**15])

In [155]: np.cosh(x)
<ipython-input-155-1e743139b88e>:1: RuntimeWarning: overflow encountered in cosh
  np.cosh(x)
Out[155]: 
array([           inf, 4.44305526e+06, 1.00000000e+00, 4.44305526e+06,
                  inf])
In [156]: 1/(np.cosh(x)**2)
<ipython-input-156-5cf76600c0c7>:1: RuntimeWarning: overflow encountered in cosh
  1/(np.cosh(x)**2)
Out[156]: 
array([0.00000000e+00, 5.06566622e-14, 1.00000000e+00, 5.06566622e-14,
       0.00000000e+00])

该警告不会阻止您获取有用的值。这是一个 警告 ,而不是 错误 .

但可以抑制警告。在路上是和 errstate :

In [157]: with np.errstate(over='ignore'):
     ...:     y = 1/(np.cosh(x)**2)
     ...: 
In [158]: y
Out[158]: 
array([0.00000000e+00, 5.06566622e-14, 1.00000000e+00, 5.06566622e-14,
       0.00000000e+00])

np.seterr 也可以使用,但它将改变整个脚本的处理方式,而不仅仅是这个上下文。所以 with np.errstate 优先考虑。

一定要花时间阅读文档。

In [159]: np.geterr()
Out[159]: {'divide': 'warn', 'over': 'warn', 'under': 'ignore', 'invalid': 'warn'}

还有一个 warnings 单元

Why can't I suppress numpy warnings