在2d NumPy数组中查找最接近的非零元素和相应的索引

问题描述 投票:1回答:1

假设我有一个名为array的2d数组和一个2d索引:(x,y),我想获取最接近(x,y)的非零元素,并获取np.nonzero(array)中元素的对应索引

python numpy
1个回答
4
投票
方法#1:这是一种方法-

def nearest_nonzero_idx(a,x,y): idx = np.argwhere(a) # If (x,y) itself is also non-zero, we want to avoid those, so delete that # But, if we are sure that (x,y) won't be non-zero, skip the next step idx = idx[~(idx == [x,y]).all(1)] return idx[((idx - [x,y])**2).sum(1).argmin()] 样品运行-

In [64]: a
Out[64]: 
array([[0, 0, 1, 1, 0, 1, 1],
       [0, 0, 1, 0, 0, 0, 1],
       [1, 1, 0, 0, 0, 0, 0],
       [0, 1, 1, 0, 0, 0, 0],
       [0, 1, 0, 0, 0, 0, 0],
       [1, 1, 0, 0, 1, 1, 0],
       [0, 1, 0, 0, 1, 0, 1],
       [1, 0, 0, 1, 1, 1, 0]])

In [65]: x,y =(3,5)

In [66]: nearest_nonzero(a,x,y)
Out[66]: array([5, 5])

方法#2:这是另一种注重性能的方法,该方法避免了通过将点临时设置为(x,y)来从非零索引数组中跳过0点的先前方法的第二步,获取那些非零索引,然后将原始值放回原值。同样,我们可以使用np.nonzero存储row, col索引,然后使用它们进行距离计算。 

因此,实现将是-

def nearest_nonzero_idx_v2(a,x,y): tmp = a[x,y] a[x,y] = 0 r,c = np.nonzero(a) a[x,y] = tmp min_idx = ((r - x)**2 + (c - y)**2).argmin() return r[min_idx], c[min_idx]

运行时测试

In [110]: a Out[110]: array([[3, 2, 3, 3, 0, 2, 4, 2, 1], [0, 3, 4, 3, 4, 3, 3, 2, 0], [1, 3, 0, 0, 0, 0, 0, 0, 0], [0, 1, 2, 0, 0, 2, 0, 0, 2], [3, 0, 0, 0, 0, 0, 0, 0, 1], [0, 0, 2, 2, 4, 4, 3, 4, 3], [2, 2, 2, 1, 0, 0, 1, 1, 1], [3, 4, 3, 1, 0, 4, 0, 4, 2]]) In [111]: x,y =(3,5) In [112]: nearest_nonzero_idx(a,x,y) Out[112]: array([1, 5]) In [113]: nearest_nonzero_idx_v2(a,x,y) Out[113]: (1, 5) In [114]: %timeit nearest_nonzero_idx(a,x,y) 10000 loops, best of 3: 23.1 µs per loop In [115]: %timeit nearest_nonzero_idx_v2(a,x,y) 100000 loops, best of 3: 4.85 µs per loop

© www.soinside.com 2019 - 2024. All rights reserved.