[英]How to find the nearest neighbor in numpy?
There are two array u and v.有两个数组 u 和 v。
u.shape = (N,d) v.shape = (q,d) u.shape = (N,d) v.shape = (q,d)
I need to find, for every q, the nearest value's index for each d in u.我需要为每个 q 找到 u 中每个 d 的最接近值的索引。
For example:例如:
u = [[5,3],
[3,4],
[3,2],
[8,7]] , shape (4,2)
v = [[1,3],
[2,4]] , shape (2,2)
and I found many people said we can do that:我发现很多人说我们可以做到:
v = v.expand_dims(v,axis=1) # reshape to (2,1,2) for broadcast
result = np.argmin(abs(v-u),axis=1) # (u-v).shape = (2,4,2)
Of course it found the nearest value's index.当然它找到了最接近的值的索引。 But, when there are two nearest value.
但是,当有两个最接近的值时。 I need to take the "second" one's index.
我需要采用“第二个”的索引。
In that case:在这种情况下:
v-u = [[[-4, 0],
[-2, -1],
[-2, 1],
[-7, -4]],
[[-3, 1],
[-1, 0],
[-1, 2],
[-6, -3]]])
along axis=1, there are two -2 in (uv)[0,:,0] and two -1 in (uv)[1,:,0] If we directly use:沿着axis=1,(uv)[0,:,0]有两个-2,(uv)[1,:,0]有两个-1如果我们直接用:
result = np.argmin(abs(v-u),axis=1)
result will be:结果将是:
array([[1, 0],
[1, 1]], dtype=int64)
It returns the indices corresponding to the first occurrence but I need the second one, i,e它返回对应于第一次出现的索引,但我需要第二个,即
array([[2, 0],
[2, 1]], dtype=int64)
Can anyone help?任何人都可以帮忙吗? Thanks!
谢谢!
If there can be at most 2 minimal values, you can retrieve indices of the last minimum.如果最多可以有 2 个最小值,则可以检索最后一个最小值的索引。
To do it:去做吧:
The whole code is:整个代码是:
u.shape[0] - 1 - np.argmin(abs(v-u)[:,::-1,:],axis=1)
Other choice , when there can be more than 2 min values, is to write a specialized version of argmin , for an 1-D input array, returning the index of the second minimal value if there are more of them:当最小值可能超过2 个时,其他选择是为一维输入数组编写argmin的专用版本,如果它们更多,则返回第二个最小值的索引:
def argmin2(arr):
ind = arr.argpartition(1)[:2]
return ind[0] if arr[ind[0]] < arr[ind[1]] else ind[1]
and then apply it to abs(vu) along axis 1 :然后沿轴1将其应用于abs(vu) :
np.apply_along_axis(argmin2, 1, abs(v-u))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.