从Scipy标记数组中快速计算索引np.wh公司

2024-03-29 12:11:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在处理一个大型数组(3000 x 3000),我在上面使用scipy.ndimage.label。返回3403个标签和带标签的数组。我想知道这些标签的索引,例如,对于标签1,我应该知道标签数组中的行和列。 所以基本上是这样

a[0] = array([[1, 1, 0, 0],
              [1, 1, 0, 2],
              [0, 0, 0, 2],
              [3, 3, 0, 0]])


indices = [np.where(a[0]==t+1) for t in range(a[1])] #where a[1] = 3  is number of labels. 

print indices
[(array([0, 0, 1, 1]), array([0, 1, 0, 1])), (array([1, 2]), array([3, 3])), (array([3, 3]), array([0, 1]))]

我想为上面提到的3403个标签创建一个索引列表。上面的方法似乎很慢。我试过使用发电机,看起来没有什么改进。你知道吗

有什么有效的方法吗?你知道吗


Tags: 方法inforisnprangescipy标签
1条回答
网友
1楼 · 发布于 2024-03-29 12:11:06

好吧,提高效率的想法是,一旦进入循环,工作就最小化。矢量化方法是不可能的,因为每个标签的元素数是可变的。因此,考虑到这些因素,这里有一个解决方案-

a_flattened = a[0].ravel()
sidx = np.argsort(a_flattened)
afs = a_flattened[sidx]
cut_idx = np.r_[0,np.flatnonzero(afs[1:] != afs[:-1])+1,a_flattened.size]
row, col = np.unravel_index(sidx, a[0].shape)
row_indices = [row[i:j] for i,j in zip(cut_idx[:-1],cut_idx[1:])]
col_indices = [col[i:j] for i,j in zip(cut_idx[:-1],cut_idx[1:])]

样本输入,输出-

In [59]: a[0]
Out[59]: 
array([[1, 1, 0, 0],
       [1, 1, 0, 2],
       [0, 0, 0, 2],
       [3, 3, 0, 0]])

In [60]: a[1]
Out[60]: 3

In [62]: row_indices # row indices
Out[62]: 
[array([0, 0, 1, 2, 2, 2, 3, 3]), # for label-0
 array([0, 0, 1, 1]),             # for label-1
 array([1, 2]),                   # for label-2    
 array([3, 3])]                   # for label-3

In [63]: col_indices  # column indices
Out[63]: 
[array([2, 3, 2, 0, 1, 2, 2, 3]), # for label-0
 array([0, 1, 0, 1]),             # for label-1
 array([3, 3]),                   # for label-2
 array([0, 1])]                   # for label-3

关闭row_indicescol_indices的第一个元素是预期的输出。每个区域的第一组代表0-th区域,因此您可能希望跳过这些区域。你知道吗

相关问题 更多 >