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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。