You can use np.pad for this:
In [1]: import numpy as np
In [2]: x = np.ones((3, 3))
In [3]: np.pad(x, [(0, 0), (0, 1)], mode='constant')
Out[3]:
array([[ 1., 1., 1., 0.],
[ 1., 1., 1., 0.],
[ 1., 1., 1., 0.]])
Each item of the pad_width (second) argument is a tuple of before, after
for each axis. I've only padded the end of the last axis, but if you
wanted to pad both "sides" of it:
In [4]: np.pad(x, [(0, 0), (1, 1)], mode='constant')
Out[4]:
array([[ 0., 1., 1., 1., 0.],
[ 0., 1., 1., 1., 0.],
[ 0., 1., 1., 1., 0.]])
Hope that helps,
-Joe
On Fri, Jan 3, 2014 at 6:58 AM, Freddie Witherden <[email protected]>wrote:
> Hi all,
>
> This should be an easy one but I can not come up with a good solution.
> Given an ndarray with a shape of (..., X) I wish to zero-pad it to have
> a shape of (..., X + K), presumably obtaining a new array in the process.
>
> My best solution this far is to use
>
> np.zeros(curr.shape[:-1] + (curr.shape[-1] + K,))
>
> followed by an assignment. However, this seems needlessly cumbersome.
> I looked at np.pad but it does not seem to provide a means of just
> padding a single axis easily.
>
> Regards, Freddie.
>
>
> _______________________________________________
> NumPy-Discussion mailing list
> [email protected]
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
>
_______________________________________________
NumPy-Discussion mailing list
[email protected]
http://mail.scipy.org/mailman/listinfo/numpy-discussion