Python numpy.nan and logical functions: wrong results(Python numpy.nan 和逻辑函数:错误的结果)
问题描述
我在尝试评估时得到了一些令人惊讶的结果可能包含 nan
值的数据的逻辑表达式(在 numpy 中定义).
I get some surprising results when trying to evaluate
logical expressions on data that might contain nan
values (as defined in numpy).
我想了解为什么会出现这种结果以及如何正确实施.
I would like to understand why this results arise and how to implement the correct way.
我不明白为什么这些表达式的计算结果是它们所做的值:
What I don't understand is why these expressions evaluate to the value they do:
from numpy import nan
nan and True
>>> True
# this is wrong.. I would expect to evaluate to nan
True and nan
>>> nan
# OK
nan and False
>>> False
# OK regardless the value of the first element
# the expression should evaluate to False
False and nan
>>> False
#ok
或
也类似:
True or nan
>>> True #OK
nan or True
>>> nan #wrong the expression is True
False or nan
>>> nan #OK
nan or False
>>> nan #OK
如何(以有效的方式)实现正确的布尔函数,同时处理 nan
值?
How can I implement (in an efficient way) the correct boolean functions, handling also nan
values?
推荐答案
您可以使用 numpy
命名空间中的谓词:
You can use predicates from the numpy
namespace:
>>> np.logical_and(True, np.nan), np.logical_and(False, np.nan)
(True, False)
>>> np.logical_and(np.nan, True), np.logical_and(np.nan, False)
(True, False)
>>>
>>> np.logical_or(True, np.nan), np.logical_or(False, np.nan)
(True, True)
>>> np.logical_or(np.nan, True), np.logical_or(np.nan, False)
(True, True)
内置布尔运算符略有不同.来自文档一个>:x and y
等价于 if x is false, then x, else y
.因此,如果第一个参数的计算结果为 False
,它们将返回它(不是它的布尔等价物,因为它是).因此:
The built-in boolean operators are slightly different. From the docs :
x and y
is equivalent to if x is false, then x, else y
. So, if the first argument evaluates to False
, they return it (not its boolean equivalent, as it were). Therefore:
>>> (None and True) is None
True
>>> [] and True
[]
>>> [] and False
[]
>>>
等
这篇关于Python numpy.nan 和逻辑函数:错误的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Python numpy.nan 和逻辑函数:错误的结果
基础教程推荐
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 筛选NumPy数组 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01