在二维数组中找到最接近的非零元素和相应的索引

问题描述:

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

+0

为你所做的发布解决方案的工作? – Divakar

方法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:这里的重点是性能的另一种方法是避免了由点暂时设定为0,让那些非零跳过从非零指数的阵列(x,y)点以前的方法的第二个步骤索引,然后将原始值放入其中。此外,我们可以使用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