当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Python numpy.allclose()用法及代码示例


numpy.allclose()函数用于查找两个数组在公差内是否按元素相等。公差值是正的,通常很小。将相对差(rtol * abs(arr2))和绝对差atol相加,以与arr1和arr2之间的绝对差进行比较。如果任一数组包含一个或多个NaN,则返回False。如果两个数组中的inf位于相同位置且具有相同符号,则将它们视为相等。

如果以下方程式为元素式True,则allclose返回True。
absolute(arr1 - arr2) <= (atol + rtol * absolute(arr2))
由于上述方程在arr1和arr2中不对称,因此,在极少数情况下,allclose(arr1,arr2)可能与allclose(arr2,arr1)不同。

用法: numpy.allclose(arr1, arr2, rtol, atol, equal_nan=False)

参数:
arr1 :[数组]输入第一个数组。
arr2 :[数组]输入第二个数组。
rtol :[float]相对公差参数。
atol :[float]绝对公差参数。
equal_nan :[bool]是否将NaN进行比较。如果为True,则将在输出数组中将arr1中的NaN等同于arr2中的NaN。

Return :[bool]如果两个数组在给定的公差范围内相等,则返回True,否则返回False。

代码1:

# Python program explaining 
# allclose() function 
  
import numpy as geek 
  
# input arrays 
in_arr1 = geek.array([5e5, 1e-7, 4.000004e6]) 
print ("1st Input array:", in_arr1)  
  
in_arr2 = geek.array([5.00001e5, 1e-7, 4e6]) 
print ("2nd Input array:", in_arr2)  
  
# setting the absolute and relative tolerance 
rtol = 1e-05
atol = 1e-08
  
res = geek.allclose(in_arr1, in_arr2, rtol, atol) 
print ("Are the two arrays are equal within the tolerance:\t", res)
输出:
1st Input array: [  5.00000000e+05   1.00000000e-07   4.00000400e+06]
2nd Input array: [  5.00001000e+05   1.00000000e-07   4.00000000e+06]
Are the two arrays are equal within the tolerance:     True


代码2:

# Python program explaining 
# allclose() function 
   
import numpy as geek 
  
# input arrays 
in_arr1 = geek.array([5e5, 1e-7, 4.000004e6]) 
print ("1st Input array:", in_arr1)  
  
in_arr2 = geek.array([5.00001e5, 1e-7, 4e6]) 
print ("2nd Input array:", in_arr2)  
  
# setting the absolute and relative tolerance 
rtol = 1e-06
atol = 1e-09
  
res = geek.allclose(in_arr1, in_arr2, rtol, atol) 
print ("Are the two arrays are equal within the tolerance:\t", res)
输出:
1st Input array: [5000000.0, 1e-07, 40000004.0]
2nd Input array: [5000001.0, 1e-07, 40000000.0]
Are the two arrays are equal within the tolerance:     True


代码3:

# Python program explaining 
# allclose() function 
   
import numpy as geek 
  
# input arrays 
in_arr1 = geek.array([5e5, 1e-7, geek.nan]) 
print ("1st Input array:", in_arr1)  
  
in_arr2 = geek.array([5e5, 1e-7, geek.nan]) 
print ("2nd Input array:", in_arr2)  
  
# setting the absolute and relative tolerance 
rtol = 1e-06
atol = 1e-09
  
res = geek.allclose(in_arr1, in_arr2, rtol, atol) 
print ("Are the two arrays are equal within the tolerance:\t", res)
输出:
1st Input array: [500000.0, 1e-07, nan]
2nd Input array: [500000.0, 1e-07, nan]
Are the two arrays are equal within the tolerance:     False


代码4:

# Python program explaining 
# allclose() function 
   
import numpy as geek 
  
# input arrays 
in_arr1 = geek.array([5e5, 1e-7, geek.nan]) 
print ("1st Input array:", in_arr1)  
  
in_arr2 = geek.array([5e5, 1e-7, geek.nan]) 
print ("2nd Input array:", in_arr2)  
  
# setting the absolute and relative tolerance 
rtol = 1e-06
atol = 1e-09
  
res = geek.allclose(in_arr1, in_arr2, rtol, atol,  
                                equal_nan = True) 
  
print ("Are the two arrays are equal within the tolerance:\t", res)
输出:
1st Input array: [500000.0, 1e-07, nan]
2nd Input array: [500000.0, 1e-07, nan]
Are the two arrays are equal within the tolerance:     True


相关用法


注:本文由纯净天空筛选整理自jana_sayantan大神的英文原创作品 numpy.allclose() in Python。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。