在numpy 2d数组的每一行中放置一些beet之间的快速方法

Har*_*Man 3 python arrays numpy matrix

我有一个2d数组(Q)只包含0和1.我希望在每行1的每个位置填充1个Q.这是一个例子:

原始矩阵:

[0 0 0 1 0 1]
[1 0 0 0 0 0]
[0 0 0 0 0 0]
[1 1 0 1 0 0]
[1 0 0 0 0 1]
[0 1 1 0 0 1]
[1 0 1 0 1 0]
Run Code Online (Sandbox Code Playgroud)

结果矩阵:

[0 0 0 1 1 1]
[1 0 0 0 0 0]
[0 0 0 0 0 0]
[1 1 1 1 0 0]
[1 1 1 1 1 1]
[0 1 1 1 1 1]
[1 1 1 1 1 0]
Run Code Online (Sandbox Code Playgroud)

我实现了一个算法,它可以工作,但对于大型数组,它效率不高.

def beetween(Q):
    for client in range(len(Q)):
        idStart = findIdStart(Q, client)
        idEnd = findIdEnd(Q, client)
        if idStart != idEnd and idStart > -1 and idEnd > -1:
             for i in range(idStart, idEnd):
                  Q[client][i] = 1
     return Q

def findIdStart(Q, client):
    if Q.ndim > 1:
        l, c = np.array(Q).shape
        for product in range (0, c):
            if Q[client][product] == 1:
                return product
    else:
        idProduct = 1
        Qtemp = Q[client]
        if Qtemp[idProduct] == 1:
            return idProduct
    return -1

def findIdEnd(Q, client):
    if Q.ndim > 1:
        l, c = np.array(Q).shape
        Qtemp = Q[client]
        for product in range(0,c):
            idProduct = (c-1)-product
            if Qtemp[idProduct]==1:
                return idProduct
    else:
        idProduct = 1
        Qtemp = Q[client]
        if Qtemp[idProduct] == 1:
            return idProduct
    return -1
Run Code Online (Sandbox Code Playgroud)

我正在尝试构建一个更优化的版本,但我没有成功:

def beetween(Q):
    l, c = np.shape(Q)
    minIndex = Q.argmax(axis=1)
    maxIndex = c-(np.fliplr(Q).argmax(axis=1))
    Q = np.zeros(shape=(l,c)).astype(np.int)
    for i in range(l):
        Q[i, minIndex[i]:maxIndex[i]] = 1
    return Q
Run Code Online (Sandbox Code Playgroud)

原始矩阵:

[0 0 0 1 0 1]
[1 0 0 0 0 0]
[0 0 0 0 0 0]
[1 1 0 1 0 0]
[1 0 0 0 0 1]
[0 1 1 0 0 1]
[1 0 1 0 1 0]
Run Code Online (Sandbox Code Playgroud)

错误的结果

[0 0 0 1 1 1] # OK
[1 0 0 0 0 0] # OK
[1 1 1 1 1 1] # wrong
[1 1 1 1 0 0] # OK
[1 1 1 1 1 1] # OK
[0 1 1 1 1 1] # OK
[1 1 1 1 1 0] # OK
Run Code Online (Sandbox Code Playgroud)

任何人都可以建议另一个简单的解决方案吗?

谢谢.

War*_*ser 5

这是一个单行:

In [25]: Q
Out[25]: 
array([[0, 0, 0, 1, 0, 1],
       [1, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0],
       [1, 1, 0, 1, 0, 0],
       [1, 0, 0, 0, 0, 1],
       [0, 1, 1, 0, 0, 1],
       [1, 0, 1, 0, 1, 0]])

In [26]: np.maximum.accumulate(Q, axis=1) & np.maximum.accumulate(Q[:,::-1], axis=1)[:,::-1]
Out[26]: 
array([[0, 0, 0, 1, 1, 1],
       [1, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0],
       [1, 1, 1, 1, 0, 0],
       [1, 1, 1, 1, 1, 1],
       [0, 1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1, 0]])
Run Code Online (Sandbox Code Playgroud)

要么

In [36]: np.minimum(np.maximum.accumulate(Q, axis=1), np.maximum.accumulate(Q[:,::-1], axis=1)[:,::-1])
Out[36]: 
array([[0, 0, 0, 1, 1, 1],
       [1, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0],
       [1, 1, 1, 1, 0, 0],
       [1, 1, 1, 1, 1, 1],
       [0, 1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1, 0]])
Run Code Online (Sandbox Code Playgroud)

在任何一种情况下,两个术语组合在一起

In [37]: np.maximum.accumulate(Q, axis=1)
Out[37]: 
array([[0, 0, 0, 1, 1, 1],
       [1, 1, 1, 1, 1, 1],
       [0, 0, 0, 0, 0, 0],
       [1, 1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1, 1],
       [0, 1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1, 1]])
Run Code Online (Sandbox Code Playgroud)

In [38]: np.maximum.accumulate(Q[:,::-1], axis=1)[:,::-1]
Out[38]: 
array([[1, 1, 1, 1, 1, 1],
       [1, 0, 0, 0, 0, 0],
       [0, 0, 0, 0, 0, 0],
       [1, 1, 1, 1, 0, 0],
       [1, 1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1, 1],
       [1, 1, 1, 1, 1, 0]])
Run Code Online (Sandbox Code Playgroud)