gpt4 book ai didi

python-3.x - 如何识别 scipy.stats.chisquare 返回负值的错误?

转载 作者:行者123 更新时间:2023-12-04 15:45:16 25 4
gpt4 key购买 nike

我在窗口 10 下使用 spyder 3.1.3 和 python 3.6.8,scipy 1.2.1。我想获取 chisquare 值,但注意到返回了负值。这是为什么?

from scipy.stats import chisquare
chisquare(f_obs=[2,1], f_exp=[100000,1])
#Power_divergenceResult(statistic=14096.65412, pvalue=0.0)

但是

chisquare(f_obs=[2,1], f_exp=[1000000,1])
#Power_divergenceResult(statistic=-731.379964, pvalue=1.0)

chisquare 中的期望值是否有上限?谢谢。

最佳答案

在 Windows 上,numpy 数组的默认整数类型是 32 位。我可以通过将 dtype np.int32 的 numpy 数组传递给 chisquare 来重现该问题:

In [5]: chisquare(f_obs=np.array([2,1], dtype=np.int32), f_exp=np.array([1000000,1], dtype=np.int32))                    
Out[5]: Power_divergenceResult(statistic=-731.379964, pvalue=1.0)

这是一个错误。我在 SciPy github 站点上为此创建了一个问题:https://github.com/scipy/scipy/issues/10159

要解决此问题,请将输入参数转换为数据类型为 numpy.int64numpy.float64 的数组:

In [6]: chisquare(f_obs=np.array([2,1], dtype=np.int64), f_exp=np.array([1000000,1], dtype=np.int64))                    
Out[6]: Power_divergenceResult(statistic=999996.000004, pvalue=0.0)

In [7]: chisquare(f_obs=np.array([2,1], dtype=np.float64), f_exp=np.array([1000000,1], dtype=np.float64))
Out[7]: Power_divergenceResult(statistic=999996.000004, pvalue=0.0)

关于python-3.x - 如何识别 scipy.stats.chisquare 返回负值的错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56069553/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com