【发布时间】:2019-07-27 17:59:54
【问题描述】:
如何比较两个大小不同但浮点数近似的数组?例如:
# I have two arrays
a = np.array( [-2.83, -2.54, ..., 0.05, ..., 2.54, 2.83] )
b = np.array( [-3.0, -2.9, -2.8, ..., -0.1, 0.0, 0.1, ..., 2.9, 3.0] )
# wherein len( b ) > len( a )
我需要的是索引 where(考虑两个列表中的这两个值)
math.isclose( -2.54, -2.5, rel_tol=1e-1) == True
我需要的答案是这样的
list_of_index_of_b = [1, 5, ..., -2]
这里的list_of_index_of_b 是一个带有“坐标”的列表,其中b 的特定元素近似于a 的某个元素。并非a 的所有元素在b 中都有近似值。还:
len(list_of_index_of_b) == len(a)
【问题讨论】:
-
len(list_of_index_of_b) == len(a) ?
-
如果
a和b不是太大,一个简单的方法是np.where(np.isclose(*np.ix_(a, b), rtol=1e-1))。
标签: python-3.x numpy floating-point compare precision