np.isnan() == False, but np.isnan() is not False(np.isnan() == False,但 np.isnan() 不是 False)
问题描述
据我了解,== 检查值是否相等,is 检查值后面的结构的身份(例如 === 其他语言).
As far as I understand it, == checks for equality of value, and is checks for identity of structure behind value (as, say === in some other languages).
鉴于此,我不明白以下内容:
Given that, I don't understand the following:
np.isnan(30) == False
Out[19]:
True
np.isnan(30) is False
Out[20]:
False
其他身份检查似乎并非如此:
It appears not to be the case with other identity checks:
(5 == 4) == False
Out[22]:
True
(5 == 4) is False
Out[23]:
True
看起来好像 np.isnan() 返回 False 作为值,而不是作为标识.为什么会这样?
It appears as if np.isnan() returns False as a value, but not as identity. Why is that the case?
推荐答案
numpy.isnan() 返回一个compatible 类型的对象:
numpy.isnan() returns a compatible type object:
>>> import numpy
>>> type(numpy.isnan(0))
<class 'numpy.bool_'>
这是一个自定义布尔值,可以有效地存储在 numpy 数组中,请参阅 Numpy 的 数据类型 文档.numpy.isnan() 函数也可以对数组进行操作,生成另一个数组并得到结果:
This is a custom boolean that can be stored efficiently in numpy arrays, see Numpy's Data Types documentation. The numpy.isnan() function can also operate on arrays, producing another array with results:
>>> numpy.isnan(numpy.array([1, 2]))
array([False, False], dtype=bool)
dtype 又是 Numpy 布尔对象.
where again the dtype is the Numpy boolean object.
Python 不保证布尔运算必须始终返回单例布尔值.你永远不应该测试 is True 或 is False anyway.在布尔运算中直接使用 numpy.isnan() 输出,使用 not 来测试假值:
Python makes no guarantees that boolean operations must always return a singleton boolean value. You should never test for is True or is False anyway. Use numpy.isnan() output directly in boolean operations, use not to test for false values:
if numpy.isnan(foo):
和
if not numpy.isnan(bar):
这篇关于np.isnan() == False,但 np.isnan() 不是 False的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:np.isnan() == False,但 np.isnan() 不是 False
基础教程推荐
- PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01
- PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01
- 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01
- 包装空间模型 2022-01-01
- 求两个直方图的卷积 2022-01-01
- Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01
- 在Python中从Azure BLOB存储中读取文件 2022-01-01
- 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
- 修改列表中的数据帧不起作用 2022-01-01
- 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
