[Numpy-discussion] Broadcasting with np.logical_and.reduce

Antony Lee antony.lee at berkeley.edu
Fri Sep 12 05:04:53 EDT 2014


I am not using asarray here.  Sorry, but I don't see how this is relevant
-- my comparison with np.add.reduce is simply that when a list of float
arrays is passed to np.add.reduce, broadcasting happens as usual, but not
when a list of bool arrays is passed to np.logical_and.reduce.

2014-09-12 0:48 GMT-07:00 Sebastian Berg <sebastian at sipsolutions.net>:

> On Do, 2014-09-11 at 22:54 -0700, Antony Lee wrote:
> > Hi,
> > I thought that ufunc.reduce performs broadcasting, but it seems a bit
> > confused by boolean arrays:
> >
> > <ipython with pylab mode on>
> > In [1]: add.reduce([array([1, 2]), array([1])])
> > Out[1]: array([2, 3])
> > In [2]: logical_and.reduce([array([True, False], dtype=bool),
> > array([True], dtype=bool)])
> >
> ---------------------------------------------------------------------------
> > ValueError                                Traceback (most recent call
> > last)
> > <ipython-input-2-bedbab4c13e1> in <module>()
> > ----> 1 logical_and.reduce([array([True, False], dtype=bool),
> > array([True], dtype=bool)])
> >
> > ValueError: The truth value of an array with more than one element is
> > ambiguous. Use a.any() or a.all()
> >
> > Am I missing something here?
> >
>
> `np.asarray([array([1, 2]), array([1])])` is an object array, not a
> boolean array. You probably want to concatenate them.
>
> - Sebastian
>
>
> > Thanks,
> > Antony
> >
> > _______________________________________________
> > NumPy-Discussion mailing list
> > NumPy-Discussion at scipy.org
> > http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20140912/04633df1/attachment.html>


More information about the NumPy-Discussion mailing list