python - 在 scipy.stats.chisquare 中处理 NaN
问题描述
我正在尝试对一些包含 NaN 的数据执行 chi^2 测试。这是一个MWE:
from scipy.stats import chisquare as chi2
import numpy as np
x = [16, 18, 16, 14, 12, 12]
chi2(x)
输出
Power_divergenceResult(statistic=2.0, pvalue=0.8491450360846096)
但
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
? 如果不是,那么处理它们的最佳方法是什么?
解决方案
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
调用之前。
现在,如果x
它ndarray
可能会起作用:)
In [249]: x = np.array([16, 18, 16, 14, 12, 12])
In [250]: x[mask]
Out[250]: array([16, 18, 16, 14, 12])
推荐阅读
- python - 如何禁用柱状列中的文本换行?
- cordova - ionic - 为 web 构建,index.html 寻找不存在的文件
- python - Python 3.7:无法在 Windows 10 的 venv 中安装 gmpy2
- symfony - 带有 Symfony 4.4.1 的 Kayue\WordpressBundle:在链配置的命名空间 App\Entity 中找不到类 XXX
- .net-core - 无法加载类型 Confluent.Kafka.Serialization.StringDeserializer
- python - 如何将flask_mysqldb与蓝图一起使用
- object-detection - 对于 360p 和 1080p,更快的 R-CNN(基于冻结推理图 inception v2)的执行时间相同。这怎么可能?
- node.js - 无法启动 pnpm 脚本 - 错误:命令失败,退出代码为 1
- javascript - 具有身份验证的 Firebase 快照侦听器
- r - 闪亮的数据表只保留选定的行..错误:未实现这些类型的比较