如何识别 scipy.stats.chisquare returns 负值错误?
How to identify error with scipy.stats.chisquare returns negative values?
我在 window 10 下使用 python 3.6.8 和 scipy 1.2.1 的 spyder 3.1.3。我想获取 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.int64
或 numpy.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)
我在 window 10 下使用 python 3.6.8 和 scipy 1.2.1 的 spyder 3.1.3。我想获取 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.int64
或 numpy.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)