[Numpy-discussion] Add pad_to_shape and pad_to_match for simpler padding if a target size is already known

czorio4 czorio4 at gmail.com
Thu Apr 15 05:54:16 EDT 2021


Hello all,

I'm only two weeks late with this message about my pull request
<https://github.com/numpy/numpy/pull/18707> that adds functions that allow
the user to pad to a target shape, instead of adding a certain amount to
each axis.

For example:

    x = np.ones((3, 3))
    # We want an output shape of (10, 10)
    padded = np.pad_to_shape(x, (10, 10))
    print(padded.shape)
    prints: (10, 10)

Whereas with the current implementation of np.pad you'd need to first
figure out the difference in axis sizes. The proposed function would
perform this step for you. As this function passes to np.pad internally, I
made sure to include the arguments in the signature that np.pad uses for
padding modes.

Finally, I've added a logical extension of this function; pad_to_match,
this takes another array and pads the input array to match.

These calls would be equivalent:

    x = np.ones((3, 3))
    y = np.zeros((10, 10))
    padded_to_shape = np.pad_to_shape(x, y.shape)
    padded_to_match = np.pad_to_match(x, y)

For additional functionality description, I refer to the pull request.

I'm not too familiar with mailing lists, so I hope this is how things work.

Kind Regards,
Mathijs
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.python.org/pipermail/numpy-discussion/attachments/20210415/971a7d66/attachment.html>


More information about the NumPy-Discussion mailing list