与南斯打交道scipy.stats.chisqu公司

2024-06-30 15:03:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图对一些包含nan的数据进行chi^2测试。以下是MWE:

from scipy.stats import chisquare as chi2
import numpy as np
x = [16, 18, 16, 14, 12, 12]
chi2(x)

输出

^{pr2}$

但是

x[-1] = np.nan
chi2(x)

给予

Power_divergenceResult(statistic=nan, pvalue=nan)

使用面膜

mask = ~np.isnan(x)
chi2(x[mask])

结果

TypeError                                 Traceback (most recent call last)
<ipython-input-13-3c009fd66f63> in <module>
----> 1 chi2(x[mask])

TypeError: only integer scalar arrays can be converted to a scalar index

我认为(希望)我实际数据中的nan是导致这个问题的原因。scipy.stats.chisquare是否有处理nan的内置方法,例如,spearmanr处理它的nan_policy?如果没有,最好的处理方法是什么?在


Tags: 数据方法importasstatsnpmaskscipy
1条回答
网友
1楼 · 发布于 2024-06-30 15:03:49

x是一个列表;布尔数组(就此而言,任何数组)不能用于索引列表。在

In [244]: x = [16, 18, 16, 14, 12, 12]                                          
In [245]: x[-1] = np.nan                                                        
In [246]: mask = ~np.isnan(x)                                                   
In [247]: x[mask]                                                               
                                     -
TypeError                                 Traceback (most recent call last)
<ipython-input-247-fee3ce9a3be1> in <module>
  > 1 x[mask]

TypeError: only integer scalar arrays can be converted to a scalar index
In [248]: mask                                                                  
Out[248]: array([ True,  True,  True,  True,  True, False])

该错误发生在调用chi2之前。在

如果xndarray它可能会工作:)

^{pr2}$

相关问题 更多 >