numpy - Zero padding multiple values in Python -


in so, solution adding single 0 between values in numpy.array:

import numpy np  arr = np.arange(1, 7)                 # array([1, 2, 3, 4, 5, 6]) np.insert(arr, slice(1, none, 2), 0)  # array([1, 0, 2, 0, 3, 0, 4, 0, 5, 0, 6]) 

how add more zeros between each value in original array? example, 5 zeros:

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

you can create 2dim array, , flatten it:

import numpy np = np.arange(1,7) num_zeros = 5 z = np.zeros((a.size, num_zeros))  np.append(a[:,np.newaxis], z, axis=1) array([[ 1.,  0.,  0.,  0.,  0.,  0.],        [ 2.,  0.,  0.,  0.,  0.,  0.],        [ 3.,  0.,  0.,  0.,  0.,  0.],        [ 4.,  0.,  0.,  0.,  0.,  0.],        [ 5.,  0.,  0.,  0.,  0.,  0.],        [ 6.,  0.,  0.,  0.,  0.,  0.]])  np.append(a[:,np.newaxis], z, axis=1).flatten() array([ 1.,  0.,  0.,  0.,  0.,  0.,  2.,  0.,  0.,  0.,  0.,  0.,  3.,         0.,  0.,  0.,  0.,  0.,  4.,  0.,  0.,  0.,  0.,  0.,  5.,  0.,         0.,  0.,  0.,  0.,  6.,  0.,  0.,  0.,  0.,  0.])  np.append(a[:,np.newaxis], z, axis=1).flatten()[:-num_zeros] array([ 1.,  0.,  0.,  0.,  0.,  0.,  2.,  0.,  0.,  0.,  0.,  0.,  3.,         0.,  0.,  0.,  0.,  0.,  4.,  0.,  0.,  0.,  0.,  0.,  5.,  0.,         0.,  0.,  0.,  0.,  6.]) 

Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -