Numpy 的 isclose(~)
对给定的两个数组执行逐元素比较,并且对于每次比较,如果两个值之间的差异落在指定的容差范围内,则返回 True。
参数
1. x1
| array-like
第一个输入数组。
2. x2
| array-like
第二个输入数组。
3. rtol
| float
| optional
相对容差参数。默认情况下,rtol=0
。
4. atol
| float
| optional
绝对公差参数。默认情况下, atol
设置为较小的数字 (~1e-8)
,因此不适合比较远小于 1 的数字。
5. equal_nan
| boolean
| optional
如果为 True,则涉及两个 NaNs 的逐元素比较将计算为 True。默认情况下,equal_nan=False
。
此处,如果满足以下条件,则逐元素比较的计算结果为 True:
absolute(a - b) <= (atol + rtol * absolute(b))
返回值
一个布尔值,指示两个数组是否足够"close"。
例子
基本用法
np.isclose([1,2], [3,2])
array([False, True])
这里,第一次逐元素比较 2 != 5
,因此该方法返回 False
。
指定绝对公差参数
np.isclose([6,4], [8,3], atol=2)
array([ True, True])
这里是 absolute(6,8) <= 2
和 absolute(4,3) <= 2
。
指定相对容差参数
np.isclose([6,3], [4,6], rtol=0.5)
array([ True, True])
这里是 absolute(6,4) <= 4*0.5
和 absolute(3,6) <= 6*0.5
。
比较NaNs
np.isclose(np.NaN, np.NaN)
False
np.isclose(np.NaN, np.NaN, equal_nan=True)
True
相关用法
- Python math isclose()用法及代码示例
- Python NumPy iscomplexobj方法用法及代码示例
- Python NumPy iscomplex方法用法及代码示例
- Python isinstance方法用法及代码示例
- Python string isidentifier()用法及代码示例
- Python calendar isleap()用法及代码示例
- Python NumPy isalnum方法用法及代码示例
- Python NumPy isnat方法用法及代码示例
- Python string isupper()用法及代码示例
- Python string isalnum()用法及代码示例
- Python Pandas isnull方法用法及代码示例
- Python isdisjoint()用法及代码示例
- Python NumPy isposinf方法用法及代码示例
- Python issubclass()用法及代码示例
- Python NumPy isreal方法用法及代码示例
- Python string istitle()用法及代码示例
- Python math isnan()用法及代码示例
- Python string isalpha()用法及代码示例
- Python NumPy isnumeric方法用法及代码示例
- Python NumPy isrealobj方法用法及代码示例
- Python NumPy isfinite方法用法及代码示例
- Python string isdigit()用法及代码示例
- Python NumPy isalpha方法用法及代码示例
- Python string isdecimal()用法及代码示例
- Python NumPy isinf方法用法及代码示例
注:本文由纯净天空筛选整理自Isshin Inada大神的英文原创作品 NumPy | isclose method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。