python - SciPy genextreme.fit 给出 : "RuntimeWarning: divide by zero"

标签 python scipy statistics

我正在尝试使用 SciPy 中的 genextreme 包进行 GEV 拟合。虽然我可以用它来估计参数,但我收到一条警告,指出它除以零。

我尝试拟合不同的数据集,尝试为形状参数 c 设置不同的初始值,还尝试将数据乘以例如100.这些都不起作用。我正在使用 Python 2.7.12 和 SciPy 0.17.0。

我是 Python 新手,想要对现有安装进行快速的首次测试。

代码中给出的数据大致是 Gumbel 分布 (c=0),因为我已经在 Gumbel 图上检查了它并使用线性回归进行了 Gumbel 拟合。

import numpy as np
from scipy.stats import genextreme as gev
t=np.array([3.8482, 3.6435, 3.0417, 4.0329, 3.2967, 3.3535, 3.6179, 3.3042, 3.6164, 3.5855, 2.7932, 2.8833, 2.6513, 2.7794, 3.2649, 3.2613, 3.1736, 3.1131, 3.3896, 4.2891])
a=gev.fit(t)

根据我的线性回归,我预计形状参数接近 c=0,位置参数接近 3.15,尺度参数接近 0.39。实际结果是:

/usr/lib/python2.7/dist-packages/scipy/stats/_continuous_distns.py:1776: RuntimeWarning: invalid value encountered in true_divide
np.sign(c)*(-g3+(g2+2*g2mg12)*g1)/((g2mg12)**(3./2.)))
/usr/lib/python2.7/dist-packages/scipy/stats/_continuous_distns.py:1781: RuntimeWarning: invalid value encountered in true_divide
(g4+(-4*g3+3*(g2+g2mg12)*g1)*g1)/((g2mg12)**2))

但是,显然将一个值分配给 a,因为:

>>> a
(0.16458924337692377, 3.1800328240261857, 0.37894174199431357)

这些值接近我的预期,但我不明白该警告。

最佳答案

问题在于您的代码进行了一些计算,其中尝试除以零或 NaN。
您可以通过深入研究各个计算并处理零和 NaN 来解决这个问题,以便您的算法跳过它们,或者您可以执行以下操作:

import numpy as np
from scipy.stats import genextreme as gev
with np.errstate(divide='ignore', invalid='ignore'):
    t=np.array([3.8482, 3.6435, 3.0417, 4.0329, 3.2967, 3.3535, 3.6179, 3.3042, 3.6164, 3.5855, 2.7932, 2.8833, 2.6513, 2.7794, 3.2649, 3.2613, 3.1736, 3.1131, 3.3896, 4.2891])
    a=gev.fit(t)

这只是通过告诉算法忽略将被 0 或 NaN 除的值作为异常处理的一种形式来抑制警告。
注意:divide 表示除以 0,invalid 表示在 numpy 数学运算中遇到 NaN。
欲了解更多信息:https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.errstate.html

关于python - SciPy genextreme.fit 给出 : "RuntimeWarning: divide by zero",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54736199/

相关文章:

python - 使用 Pandas 库将日期/时间转换为月份后获取 float 而不是整数

python - 多元线性回归中的归一化

python - 有人在 Matplotlib 中制作了 Parula 颜色图吗?

python - 如何使用 Numpy/Scipy 编写一致的代码?

python - Python中的_winreg.CreateKey问题

python - 如何对颜色进行线性插值?

python - 如何创建新的 pandas 列 scipy.optimize 返回值

python - 列中的多个分类变量和准备

python - SQLAlchemy 引发 None,导致 TypeError

Python Matplotlib : Just the graph image