非常感谢您提供有关此问题的帮助。看起来应该很简单。

我在名为Totals的熊猫数据框中有两列:Totals ['Connections']和Totals ['Expected']。 Totals ['Connections']包含观察到的相关变量的发生次数; Totals ['Expected']包含预期的观察数。我想使用scipy chisquare函数比较两者。我通过以下方式执行此操作:

sp.stats.chisquare([Totals.Connections], f_exp=[Totals.Expected])


但是,当我这样做时,我得到了有效的测试统计量,但是我的p值却是“ nan”,如下所示(请参见下文)。另外,结果开头的“ Power_divergence”文本是什么意思?谁能解释我在这里做错了什么?

Power_divergenceResult(statistic=array([  1.05408049e+03,   6.30832196e+02,   7.02987722e+01,
         9.17326057e+00,   1.56193724e+01,   3.36275580e+01,
         6.16076398e+02,   1.50373806e+02,   2.94802183e+01,
         2.65321965e+02,   1.00900409e+01,   3.06515689e+02,
         1.38828104e+02,   3.68894952e+02,   1.92873124e+02,
         5.67564802e+02,   2.36092769e+02,   1.77298772e+03,
         3.55388267e+03,   6.42013643e+02,   1.55858117e+02,
         1.22783083e+02,   1.36425648e-03,   2.47579809e+02,
         2.36092769e+02,   7.02987722e+01,   1.23124147e+01,
         6.10587995e+02,   2.75088677e+01,   2.76261937e+02,
         2.00121419e+02,   4.97702592e+02,   2.01167804e+02,
         1.26909959e+02,   2.60530696e+02,   6.66316508e+01,
         2.15019100e+02]), pvalue=array([ nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,
        nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,
        nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,  nan,
        nan,  nan,  nan,  nan]))

最佳答案

看起来您希望每个参数都是一维的,但是在参数周围有多余的括号,这为每个参数增加了额外的维度。删除那些多余的括号:

sp.stats.chisquare(Totals.Connections, f_exp=Totals.Expected)


例如,这是chisquare的典型用法:

In [49]: chisquare([4, 4, 5, 5], [4, 3, 7, 4])
Out[49]: Power_divergenceResult(statistic=1.1547619047619047, pvalue=0.76387343970439647)


如果将参数包装在额外的括号中,它们将变为二维,并且chisquare将应用于每个(平凡的)列(因为默认值为axis=0):

In [50]: chisquare([[4, 4, 5, 5]], [[4, 3, 7, 4]])
Out[50]: Power_divergenceResult(statistic=array([ 0.        ,  0.33333333,  0.57142857,  0.25      ]), pvalue=array([ nan,  nan,  nan,  nan]))


该计算与调用chisquare四次相同,对于参数的每一列一次。当参数的长度仅为1时,p值为nan

In [59]: chisquare([4], [4])
Out[59]: Power_divergenceResult(statistic=0.0, pvalue=nan)

In [60]: chisquare([4], [3])
Out[60]: Power_divergenceResult(statistic=0.33333333333333331, pvalue=nan)

In [61]: chisquare([5], [7])
Out[61]: Power_divergenceResult(statistic=0.5714285714285714, pvalue=nan)

In [62]: chisquare([5], [4])
Out[62]: Power_divergenceResult(statistic=0.25, pvalue=nan)


为了获得预期的结果,同时保留多余的括号,您必须使用axis=1

In [63]: chisquare([[4, 4, 5, 5]], [[4, 3, 7, 4]], axis=1)
Out[63]: Power_divergenceResult(statistic=array([ 1.1547619]), pvalue=array([ 0.76387344]))

关于python - 在scipy chisquare中获取p值的nan:不知道为什么?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40380217/

10-08 21:38