-
-
Notifications
You must be signed in to change notification settings - Fork 11.1k
Open
Labels
Description
Proposed new feature or change:
It is common to want to pad an array along a specific axis. Examples:
- https://stackoverflow.com/questions/72106542/how-to-pad-the-i-j-axes-of-a-3d-np-array-without-padding-its-k-axis
- https://stackoverflow.com/questions/56076094/zero-pad-ndarray-along-axis
- https://stackoverflow.com/questions/66225047/padding-a-3-dimensional-numpy-array-with-the-medians-alongside-specific-axis
- https://stackoverflow.com/questions/19349410/how-to-pad-with-zeros-a-tensor-along-some-axis-python
- https://stackoverflow.com/questions/74966041/how-to-pad-a-specific-dimension-of-a-numpy-array
Doing so with numpy.pad
requires constructing a list of pairs of length equal to the ndim of the array, with exactly one of those pairs at the right position containing the desired pad widths. This can be verbose and cumbersome when there are several axes.
I propose a new, more user-friendly way to pad along a specific axis (or axes): Let the pad_width
argument accept a dictionary whose keys are axes and whose values are the (before, after)
pair (or perhaps single number) for the corresponding axis. Example:
# before
np.pad(array, [(0, 0), (0, 0), (1, 2), (0, 0), (0, 0)])
# after
np.pad(array, {-3: (1, 2)})
This should require only minor modification to the implementation of numpy.pad
. If others like this idea, I can create a PR for it.