numpy用有效地跨轴的连续元素之和替换2d bool数组

时间:2019-04-09 05:42:01

标签: python numpy cumsum

我有一个布尔数组(bool_arr),我想用其计数(consecutive_count)替换列上的连续非零数字(这也是连续组)

bool_arr =            consecutive_count = 
[[1 1 1 1 0 1]        [[3 6 1 6 0 1]
 [1 1 0 1 1 0]         [3 6 0 6 5 0]
 [1 1 1 1 1 1]         [3 6 3 6 5 2]
 [0 1 1 1 1 1]         [0 6 3 6 5 2]
 [1 1 1 1 1 0]         [2 6 3 6 5 0]
 [1 1 0 1 1 1]]        [2 6 0 6 5 1]]

我创建了自己的函数,该函数沿列获取连续的非零元素的累积和

consecutive_cumsum = 
[[1 1 1 1 0 1]
 [2 2 0 2 1 0]
 [3 3 1 3 2 1]
 [0 4 2 4 3 2]
 [1 5 3 5 4 0]
 [2 6 0 6 5 1]]

我目前使用以下方法获取consecutive_count

bool_arr = np.array([[1,1,1,1,0,1],
                     [1,1,0,1,1,0],
                     [1,1,1,1,1,1],
                     [0,1,1,1,1,1],
                     [1,1,1,1,1,0],
                     [1,1,0,1,1,1]])

consecutive_cumsum = np.array([[1,1,1,1,0,1],
                               [2,2,0,2,1,0],
                               [3,3,1,3,2,1],
                               [0,4,2,4,3,2],
                               [1,5,3,5,4,0],
                               [2,6,0,6,5,1]])

consecutive_count = consecutive_cumsum.copy()
for x in range(consecutive_count.shape[1]):
    maximum = 0
    for y in range(consecutive_count.shape[0]-1, -1, -1):
        if consecutive_cumsum[y,x] > 0:
            if consecutive_cumsum[y,x] < maximum: consecutive_count[y,x] = maximum
            else: maximum = consecutive_cumsum[y,x]
        else: maximum = 0

print(consecutive_count)

效果很好,但我正在遍历每个元素,以最大值替换为零。

有没有一种方法可以使用numpy将其向量化,而不是遍历所有元素。另外,指定在哪个轴(行与列)上执行

3 个答案:

答案 0 :(得分:3)

append的新prependnp.diff关键字(我相信是v1.15.0)使此操作变得容易:

bnd = np.diff(bool_arr, axis=0, prepend=0, append=0)
x, y = np.where(bnd.T)
bnd.T[x, y] *= (y[1::2]-y[::2]).repeat(2)
bnd[:-1].cumsum(axis=0)
# array([[3, 6, 1, 6, 0, 1],
#        [3, 6, 0, 6, 5, 0],
#        [3, 6, 3, 6, 5, 2],
#        [0, 6, 3, 6, 5, 2],
#        [2, 6, 3, 6, 5, 0],
#        [2, 6, 0, 6, 5, 1]])

具有可选轴:

def count_ones(a, axis=-1):
    a = a.swapaxes(-1, axis)
    bnd = np.diff(a, axis=-1, prepend=0, append=0)
    *idx, last = np.where(bnd)
    bnd[(*idx, last)] *= (last[1::2]-last[::2]).repeat(2)
    return bnd[..., :-1].cumsum(axis=-1).swapaxes(-1, axis)

更新:以及适用于常规(不仅是0/1)条目的版本:

def sum_stretches(a, axis=-1):
    a = a.swapaxes(-1, axis)
    dtype = np.result_type(a, 'i1')
    bnd = np.diff((a!=0).astype(dtype), axis=-1, prepend=0, append=0)
    *idx, last = np.where(bnd)
    A = np.concatenate([np.zeros((*a.shape[:-1], 1), a.dtype), a.cumsum(axis=-1)], -1)[(*idx, last)]
    bnd[(*idx, last)] *= (A[1::2]-A[::2]).repeat(2)
    return bnd[..., :-1].cumsum(axis=-1).swapaxes(-1, axis)

答案 1 :(得分:1)

使用itertools.groupby

import itertools

for i in range(b.shape[1]):
    counts = []
    for k,v in itertools.groupby(b[:,i]):
        g = list(v)
        counts.extend([sum(g)] * len(g))    
    b[:,i] = counts   

输出:

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

答案 2 :(得分:0)

paulpanzer的答案为基础(如我)没有麻木v1.15 +的可怜人

def sum_stretches(a, axis=-1):
    a = a.swapaxes(-1, axis)
    padding = [[0,0].copy()]*a.ndim
    padding[-1] = [1,1]
    padded = np.pad((a!=0), padding, 'constant', constant_values=0).astype('int32')
    bnd = np.diff(padded, axis=-1)
    *idx, last = np.where(bnd)
    A = np.concatenate([np.zeros((*a.shape[:-1], 1), 'int32'), a.cumsum(axis=-1)], -1)[(*idx, last)]
    bnd[(*idx, last)] *= (A[1::2]-A[::2]).repeat(2)
    return bnd[..., :-1].cumsum(axis=-1).swapaxes(-1, axis)