【问题标题】:Faster index computation from Scipy labelled array apart from np.where除了 np.where 之外,Scipy 标记数组的索引计算速度更快
【发布时间】:2017-07-29 15:00:53
【问题描述】:

我正在处理一个大型阵列 (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 标签创建一个索引列表。上面的方法似乎很慢。我尝试使用生成器,看起来没有改进。

有什么有效的方法吗?

【问题讨论】:

  • 向我们展示您的工作循环解决方案。
  • @Divakar indices = [np.where(m[0]==t+1) for t in range(m[1])] 其中 m[0] 是标记数组,m[ 1] 是标签计数(此处为 3403)。
  • 请将这些添加到问题中。也很高兴看到示例数据。
  • @Divakar 这就是想法
  • 发布的解决方案对您有用吗?

标签: python arrays numpy scipy ndimage


【解决方案1】:

嗯,提高效率的想法是在循环中尽量减少工作。鉴于每个标签的元素数量可变,因此不可能使用矢量化方法。因此,考虑到这些因素,这里有一个解决方案 -

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 区域,因此您可能想跳过这些。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-21
    • 1970-01-01
    • 1970-01-01
    • 2015-10-29
    • 1970-01-01
    • 2017-04-01
    • 2012-12-05
    • 1970-01-01
    相关资源
    最近更新 更多