zero padding numpy array zero padding numpy array numpy numpy

zero padding numpy array


As numpy have to know size of an array just prior to its initialization, best solution would be a numpy based constructor for such case. Sadly, as far as I know, there is none.

Probably not ideal, but slightly faster solution will be create numpy array with zeros and fill with list values.

import numpy as npdef pad_list(lst):    inner_max_len = max(map(len, lst))    map(lambda x: x.extend([0]*(inner_max_len-len(x))), lst)    return np.array(lst)def apply_to_zeros(lst, dtype=np.int64):    inner_max_len = max(map(len, lst))    result = np.zeros([len(lst), inner_max_len], dtype)    for i, row in enumerate(lst):        for j, val in enumerate(row):            result[i][j] = val    return result

Test case:

>>> pad_list([[ 1, 2, 3], [2], [2, 4]])array([[1, 2, 3],       [2, 0, 0],       [2, 4, 0]])>>> apply_to_zeros([[ 1, 2, 3], [2], [2, 4]])array([[1, 2, 3],       [2, 0, 0],       [2, 4, 0]])

Performance:

>>> timeit.timeit('from __main__ import pad_list as f; f([[ 1, 2, 3], [2], [2, 4]])', number = 10000)0.3937079906463623>>> timeit.timeit('from __main__ import apply_to_zeros as f; f([[ 1, 2, 3], [2], [2, 4]])', number = 10000)0.1344289779663086


Not strictly a function from numpy, but you could do something like this

from itertools import izip, izip_longestimport numpya=[[1,2,3], [4], [5,6]]res1 = numpy.array(list(izip(*izip_longest(*a, fillvalue=0))))

or, alternatively:

res2=numpy.array(list(izip_longest(*a, fillvalue=0))).transpose()

If you use python 3, use zip, and itertools.zip_longest.