ENH: Introduce multiple pair parameters in the 'repeat' function #23937
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This feature enables pairs of parameters to be passed to the NumPy's repeat function: tuple arguments can now be passed to the 'repeats' parameter and the ‘axis’ parameter can now also receive a sequence of integers. Data types are properly checked. Flattened output arrays (non-specified axes) and repeats broadcasted to the size of the paired axis are also taken into account. For instance, if two pairs of arguments are used and the second one doesn't have an axis specified, a flat output array is returned. This feature was first suggested in #21435.
Moreover, the multiple repeats are processed in ascending order, meaning the repeats that result in a smaller size of its axis in the intermediate output array are processed first. This adjustment renders a processing time reduction of approximately 50% in significantly large repeats (i.e. over 100 repeats per axis).
This enhancement makes the repeat function more versatile and elegant. The greater the number of dimensions of the input array to be repeated over a axis, the more useful this feature is.
Usage example:
>>> x = np.array([[1,2],[3,4]])
>>> x = np.repeat(x, ([3, 3], [1, 2]), (1, 0))
array([[1, 1, 1, 2, 2, 2],
[3, 3, 3, 4, 4, 4],
[3, 3, 3, 4, 4, 4]])
>>> x = np.repeat(x, (3, [1, 2], 1), (1, 0))
array([1, 1, 1, 2, 2, 2,
3, 3, 3, 4, 4, 4,
3, 3, 3, 4, 4, 4])