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

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

问题描述

我正在尝试将低于阈值的数组成员设置为 nan.这是 QA/QC 流程的一部分,传入的数据可能已经具有 nan 的时隙.

例如,我的阈值可能是 -1000,因此我想在以下数组中将 -3000 设置为 nan

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

以下内容:

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

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

warnings.filterwarnings("忽略")...warnints.resetwarnings()

有点重,可能有点不安全.

尝试使用花哨的索引进行两次索引,如下所示不会产生任何效果:

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

我认为这是因为整数索引或两次使用索引而进行了复制.

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

解决方案

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

<预><代码>>>>×<-1000数组([假,假,假,真,假,假],dtype=bool)

因此,您可以简单地忽略数组中已经存在 NaN 的事实,然后执行以下操作:

<预><代码>>>>x[x<-1000] = np.nan>>>X数组([南,1.,2.,南,南,5.])

编辑我在运行上面的代码时没有看到任何警告,但是如果您真的需要远离 NaN,您可以执行以下操作:

mask = ~np.isnan(x)掩码[掩码] &= x[掩码] <-1000x[掩码] = np.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.

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.])

This following:

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

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.

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.

解决方案

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)

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.])

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天全站免登陆