Search code examples
pythonnumpyzero-padding

Zero pad array based on other array's shape


I've got K feature vectors that all share dimension n but have a variable dimension m (n x m). They all live in a list together.

to_be_padded = []

to_be_padded.append(np.reshape(np.arange(9),(3,3)))

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

to_be_padded.append(np.reshape(np.arange(18),(3,6)))

array([[ 0,  1,  2,  3,  4,  5],
       [ 6,  7,  8,  9, 10, 11],
       [12, 13, 14, 15, 16, 17]])

to_be_padded.append(np.reshape(np.arange(15),(3,5)))

array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14]])

What I am looking for is a smart way to zero pad the rows of these np.arrays such that they all share the same dimension m. I've tried solving it with np.pad but I have not been able to come up with a pretty solution. Any help or nudges in the right direction would be greatly appreciated!

The result should leave the arrays looking like this:

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

array([[ 0,  1,  2,  3,  4,  5],
       [ 6,  7,  8,  9, 10, 11],
       [12, 13, 14, 15, 16, 17]])

array([[ 0,  1,  2,  3,  4, 0],
       [ 5,  6,  7,  8,  9, 0],
       [10, 11, 12, 13, 14, 0]])

Solution

  • You could use np.pad for that, which can also pad 2-D arrays using a tuple of values specifying the padding width, ((top, bottom), (left, right)). For that you could define:

    def pad_to_length(x, m):
        return np.pad(x,((0, 0), (0, m - x.shape[1])), mode = 'constant')
    

    Usage

    You could start by finding the ndarray with the highest amount of columns. Say you have two of them, a and b:

    a = np.array([[0, 1, 2],
           [3, 4, 5],
           [6, 7, 8]])
    
    b = np.array([[ 0,  1,  2,  3,  4],
           [ 5,  6,  7,  8,  9],
           [10, 11, 12, 13, 14]])
    
    m = max(i.shape[1] for i in [a,b])
    # 5
    

    And then use this parameter to pad the ndarrays:

    pad_to_length(a, m)
    array([[0, 1, 2, 0, 0],
           [3, 4, 5, 0, 0],
           [6, 7, 8, 0, 0]])