在一个二维的Numpy数组中,找到连续1的最大长度。

5
我有一个2d的numpy数组。 我希望找到每行连续1的最大数量。
a = np.array([[1, 1, 1, 1, 1],
              [1, 0, 1, 0, 1],
              [1, 1, 0, 1, 0],
              [0, 0, 0, 0, 0],
              [1, 1, 1, 0, 1],
              [1, 0, 0, 0, 0],
              [0, 1, 1, 0, 0],
              [1, 0, 1, 1, 0],
              ]
             )

期望输出:[5, 1, 2, 0, 3, 1, 2, 2]

我已经找到了解决上述问题的方法,适用于一维数组:

a = np.array([1, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0])
d = np.diff(np.concatenate(([0], a, [0])))
np.max(np.flatnonzero(d == -1) - np.flatnonzero(d == 1))
> 4

类似的,我写了下面这段代码,但它并没有生效。

d = np.diff(np.column_stack(([0] * a.shape[0], a, [0] * a.shape[0])))
np.max(np.flatnonzero(d == -1) - np.flatnonzero(d == 1))
1个回答

6

你当前代码的二维等效版本将使用 pad, diff, wheremaximum.reduceat

# pad with a column of 0s on left/right
# and get the diff on axis=1
d = np.diff(np.pad(a, ((0,0), (1,1)), constant_values=0), axis=1)

# get row/col indices of -1
row, col = np.where(d==-1)

# get groups of rows
val, idx = np.unique(row, return_index=True)

# subtract col indices of -1/1 to get lengths
# use np.maximum.reduceat to get max length per group of rows
out = np.zeros(a.shape[0], dtype=int)
out[val] = np.maximum.reduceat(col-np.where(d==1)[1], idx)

输出:array([5, 1, 2, 0, 3, 1, 2, 2])

中间结果:

np.pad(a, ((0,0), (1,1)), constant_values=0)

array([[0, 1, 1, 1, 1, 1, 0],
       [0, 1, 0, 1, 0, 1, 0],
       [0, 1, 1, 0, 1, 0, 0],
       [0, 0, 0, 0, 0, 0, 0],
       [0, 1, 1, 1, 0, 1, 0],
       [0, 1, 0, 0, 0, 0, 0],
       [0, 0, 1, 1, 0, 0, 0],
       [0, 1, 0, 1, 1, 0, 0]])


np.diff(np.pad(a, ((0,0), (1,1)), constant_values=0), axis=1)

array([[ 1,  0,  0,  0,  0, -1],
       [ 1, -1,  1, -1,  1, -1],
       [ 1,  0, -1,  1, -1,  0],
       [ 0,  0,  0,  0,  0,  0],
       [ 1,  0,  0, -1,  1, -1],
       [ 1, -1,  0,  0,  0,  0],
       [ 0,  1,  0, -1,  0,  0],
       [ 1, -1,  1,  0, -1,  0]])


np.where(d==-1)

(array([0, 1, 1, 1, 2, 2, 4, 4, 5, 6, 7, 7]),
 array([5, 1, 3, 5, 2, 4, 3, 5, 1, 3, 1, 4]))


col-np.where(d==1)[1]

array([5, 1, 1, 1, 2, 1, 3, 1, 1, 2, 1, 2])


np.unique(row, return_index=True)

(array([0, 1, 2, 4, 5, 6, 7]),
 array([ 0,  1,  4,  6,  8,  9, 10]))


out = np.zeros(a.shape[0], dtype=int)

array([0, 0, 0, 0, 0, 0, 0, 0])


out[val] = np.maximum.reduceat(col-np.where(d==1)[1], idx)

array([5, 1, 2, 0, 3, 1, 2, 2])

你说得对,我已经修复了它,我还纠正了填充 ;) - mozway
np.column_stack和pad做的事情是一样的,对吗? - Abhishek Jain
是的,但pad会自动处理值的数量。 - mozway

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接