Numpy xor-shrink array

How to xor all elements of a boolean numpy array using vectorized methods: i.e. a_1 xor a_2 xor ... xor a_n

?

+3


source to share


2 answers


I would rather use xor ufunc, I think that bitwise_xor

(or logical_xor

):

np.bitwise_xor.reduce(a)

      

or



np.logical_xor.reduce(a)

      

One benefit is that you don't get bogus things to float.

+6


source


Probably most efficient to use sum

:



np.sum(arr) % 2

      

+2


source







All Articles