numpy数组与nan到标量的不等式比较 [英] inequality comparison of numpy array with nan to a scalar

查看:150
本文介绍了numpy数组与nan到标量的不等式比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将低于阈值的数组成员设置为nan。这是质量保证/质量控制过程的一部分,传入的数据可能已经有插槽纳米。

I am trying to set members of an array that are below a threshold to nan. This is part of a QA/QC process and the incoming data may already have slots that are nan.

所以作为一个例子,我的阈值可能是-1000,因此我会想要在以下数组中将-3000设置为nan

So as an example my threshold might be -1000 and hence I would want to set -3000 to nan in the following array

x = np.array([np.nan,1.,2.,-3000.,np.nan,5.])

以下内容:

x[x < -1000.] = np.nan

产生正确的行为,但也是RuntimeWarning,但开销禁用警告

produces the correct behavior, but also a RuntimeWarning, but the overhead of disabling the warning

warnings.filterwarnings("ignore")
...
warnints.resetwarnings()

有点沉重,可能有点不安全。

is kind of heavy an potentially a bit unsafe.

尝试使用花式索引进行两次索引,如下所示不会产生任何影响:

Trying to index twice with fancy indexing as follows doesn't produce any effect:

nonan = np.where(~np.isnan(x))[0]
x[nonan][x[nonan] < -1000.] = np.nan

我认为这是因为整数是由于整数而产生的索引或使用索引两次。

I assume this is because a copy is made due to the integer index or the use of indexing twice.

有没有人有一个相对简单的解决方案?在这个过程中使用一个蒙面数组会很好,但最终的产品必须是一个ndarray,我不能引入新的依赖。谢谢。

Does anyone have a relatively simple solution? It would be fine to use a masked array in the process, but the final product has to be an ndarray and I can't introduce new dependencies. Thanks.

推荐答案

任何比较(除了!= ) NaN到非NaN值将始终返回False:

Any comparison (other than !=) of a NaN to a non-NaN value will always return False:

>>> x < -1000
array([False, False, False,  True, False, False], dtype=bool)

所以你可以简单地忽略你的数组中已经存在NaN的事实:

So you can simply ignore the fact that there are NaNs already in your array and do:

>>> x[x < -1000] = np.nan
>>> x
array([ nan,   1.,   2.,  nan,  nan,   5.])

编辑我在上面运行时没有看到任何警告,但是如果你真的需要远离NaN,你可以做类似的事情:

EDIT I don't see any warning when I ran the above, but if you really need to stay away from the NaNs, you can do something like:

mask = ~np.isnan(x)
mask[mask] &= x[mask] < -1000
x[mask] = np.nan

这篇关于numpy数组与nan到标量的不等式比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆