[Numpy-discussion] Applying logical operations along an axis of a boolean array?

Sebastian Berg sebastian at sipsolutions.net
Mon Dec 18 09:58:58 EST 2017


On Mon, 2017-12-18 at 12:02 +0100, hanno_lists at gmx.net wrote:
> Hi,
>  
> is it possible, to apply a logical operation, such as AND or OR along
> a particular axis of a numpy array?
> 

As mentioned, `np.any` and `np.all` work. However, what is more/also
interesting to you is maybe that:

`np.logical_or.reduce`

works. All binary ufuncs (most elementwise functions such as addition,
subtraction, multiplication, etc. support this `reduce` (and some
other, please find out yourself ;)) methods.
So that thing like `any`, `sum`, or `cumsum` are actually just thin
wrappers around those.

- Sebastian


>  
> Let's say I have an (n,m) array and I want to AND along the first
> axis, such that I get a (1,m) (or just (m,) dimensional array back. I
> looked at the documentation for np.logical_and and friends but
> couldn't find an axis keyword on the logical_xyz operations and
> nothing else seemed to fit.
>  
> Thanks, and best regards,
> Hanno
>  
>  
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at python.org
> https://mail.python.org/mailman/listinfo/numpy-discussion
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 801 bytes
Desc: This is a digitally signed message part
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20171218/3392e50d/attachment-0001.sig>


More information about the NumPy-Discussion mailing list