Zero pad array based on other array's shape

余生颓废 提交于 2020-08-17 06:42:27

问题


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]])

回答1:


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]])



回答2:


I believe there is no very efficient solution for this. I think you will need to loop over the list with a for loop and treat every array individually:

for i in range(len(to_be_padded)):
    padded = np.zeros((n, maxM))
    padded[:,:to_be_padded[i].shape[1]] = to_be_padded[i]
    to_be_padded[i] = padded

where maxM is the longest m of the matrices in your list.



来源:https://stackoverflow.com/questions/54591005/zero-pad-array-based-on-other-arrays-shape

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!