[Numpy-discussion] Add a function to broadcast arrays to a given shape to numpy's stride_tricks?
njs at pobox.com
Wed Dec 10 19:00:36 EST 2014
On Sun, Dec 7, 2014 at 7:10 AM, Stephan Hoyer <shoyer at gmail.com> wrote:
> I recently wrote function to manually broadcast an ndarray to a given shape
> according to numpy's broadcasting rules (using strides):
> The same functionality can be done pretty straightforwardly with
> np.broadcast_arrays, but that function does both too much (I don't actually
> have a second array that needs to be broadcast) and not enough (I need to
> create a dummy array to broadcast against it).
> This approach is simpler, and also, according to my benchmarks, about 3x
> faster than np.broadcast_arrays:
> In : import xray
> In : import numpy as np
> In : x = np.random.randn(4)
> In : y = np.empty((2, 3, 4))
> In : %timeit xray.core.utils.as_shape(x, y.shape)
> 100000 loops, best of 3: 17 µs per loop
> In : %timeit np.broadcast_arrays(x, y)
> 10000 loops, best of 3: 47.4 µs per loop
> Would this be a welcome addition to numpy's lib.stride_tricks? If so, I will
> put together a PR.
> In my search, I turned up a Stack Overflow post looking for similar
Seems like a useful addition to me -- I've definitely wanted this in
the past. I agree with Stephan that reshape() might not be the best
place, though; I wouldn't think to look for it there.
Two API ideas, which are not mutually exclusive:
1) Give broadcast_arrays an extra_shapes=[shape1, shape2, ...]
argument. Each entry in that list is a tuple of integers;
broadcast_arrays chooses the final output shape "as if" additional
arrays with the given shapes had been passed in, in addition to the
ones in *args.
2) Add a broadcast_to(arr, shape) function, which broadcasts the array
to exactly the shape given, or else errors out if this is not
Given (1), (2) could just be:
def broadcast_to(arr, shape):
output = broadcast_arrays(arr, extra_shapes=[shape])
if output.shape != shape:
Nathaniel J. Smith
Postdoctoral researcher - Informatics - University of Edinburgh
More information about the NumPy-Discussion