[Numpy-discussion] Should concatenate broadcast shapes?
Jaime Fernández del Río
jaime.frio at gmail.com
Wed Aug 27 13:12:59 EDT 2014
On Wed, Aug 27, 2014 at 10:01 AM, Robert Kern <robert.kern at gmail.com> wrote:
> 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.
>
That makes sense, I kind of figured there had to be a reason. So though it
may be beating a dead horse, perhaps adding a `broadcast=False` argument to
the function would do the trick? No side effects unless you ask for them,
in which case you had it coming...
Jaime
--
(\__/)
( O.o)
( > <) Este es Conejo. Copia a Conejo en tu firma y ayúdale en sus planes
de dominación mundial.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20140827/da268b93/attachment.html>
More information about the NumPy-Discussion
mailing list