简体   繁体   English

Numpy 检查两个 arrays 的元素大致相等的位置

[英]Numpy check where elements of two arrays are approximately equal

I have two numpy arrays with floating point values and I am trying to find the indices where the numbers are approximately equal (floating point comparisons).我有两个具有浮点值的 numpy arrays 并且我试图找到数字大致相等的索引(浮点比较)。

So something like:所以像:

x = np.random.rand(3)
y = np.random.rand(3)

x[2] = y[2]

# Do the comparison and it should return 2 as the index

I tried something like我尝试了类似的东西

np.where(np.allclose(x, y))

However, this returns an empty array.但是,这会返回一个空数组。 If I do:如果我做:

np.where(x == y)  # This is fine.

I tried using a combination of numpy.where and numpy.allclose but could not make it work.我尝试使用numpy.wherenumpy.allclose的组合,但无法使其工作。 Of course, I can do it with a loop but that seems tedious and unpythonic.当然,我可以使用循环来完成,但这似乎很乏味且不符合标准。

你要找的是np.isclose

np.where(np.isclose(x, y))

你可以随时使用依赖的东西:

np.where( np.abs(x-y) < epsilon )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM