Skip to content

Instantly share code, notes, and snippets.

@JDWarner
Created May 1, 2013 05:37
Show Gist options
  • Save JDWarner/5493906 to your computer and use it in GitHub Desktop.
Save JDWarner/5493906 to your computer and use it in GitHub Desktop.
Utility function for padding an array by appending `pad_amt` zeros along any `axis`. Non-symmetric. The ideas behind this snippet led to the `np.pad` improvements introduced in NumPy 1.8. This is licensed under the modified BSD.
import numpy as np
def _pad_asymmetric_zeros(arr, pad_amt, axis=-1):
"""Pads `arr` by `pad_amt` along specified `axis`"""
if axis == -1:
axis = arr.ndim - 1
zeroshape = tuple([x if i != axis else pad_amt
for (i, x) in enumerate(arr.shape)])
return np.concatenate((arr, np.zeros(zeroshape, dtype=arr.dtype)),
axis=axis)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment