[Numpy-discussion] Should concatenate broadcast shapes?
Robert Kern
robert.kern at gmail.com
Wed Aug 27 13:01:15 EDT 2014
On Wed, Aug 27, 2014 at 5:44 PM, Jaime Fernández del Río
<jaime.frio at gmail.com> wrote:
> After reading this stackoverflow question:
>
> http://stackoverflow.com/questions/25530223/append-a-list-at-the-end-of-each-row-of-2d-array
>
> I was reminded that the `np.concatenate` family of functions do not
> broadcast the shapes of their inputs:
>
>>>> import numpy as np
>>>> a = np.arange(6).reshape(3, 2)
>>>> b = np.arange(6, 8)
>>>> np.concatenate((a, b), axis=1)
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> ValueError: all the input arrays must have same number of dimensions
>>>> np.concatenate((a, b[None]), axis=1)
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> ValueError: all the input array dimensions except for the concatenation axis
> must match exactly
>>>> np.concatenate((a, np.tile(b[None], (a.shape[0], 1))), axis=1)
> array([[0, 1, 6, 7],
> [2, 3, 6, 7],
> [4, 5, 6, 7]])
In my experience, when I get that ValueError, it has usually been a
legitimate error on my part and broadcasting would not have
accomplished what I wanted. Typically, I forgot to transpose
something. If we allowed broadcasting, my most common source of errors
using these functions would silently do something unintended.
a = np.arange(6).reshape(3, 2)
b = np.arange(6, 9) # b.shape == (3,)
# I *intend* to append b as a new column, but forget to make b.shape==(3,1)
c = np.hstack([a, b])
# If hstack() doesn't broadcast, that will fail and show me my error.
# If it does broadcast, it "succeeds" but gives me something I didn't want:
array([[0, 1, 6, 7, 8],
[2, 3, 6, 7, 8],
[4, 5, 6, 7, 8]])
--
Robert Kern
More information about the NumPy-Discussion
mailing list