Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Adding boolean Numpy arrays

I have three lists as such:

a = np.array([True, True, False, False])
b = np.array([False, False, False, False])
c = np.array([False, False, False, True])

I want to add the arrays so that the new array only has False if all the corresponding elements are False. For example, the output should be:

d = np.array([True, True, False, True])

However, d = np.add(a,b,c) returns:

d = np.array([True, True, False, False])

Why is this and how can I fix it? Thanks!

like image 281
Joe Flip Avatar asked Dec 01 '22 20:12

Joe Flip


1 Answers

np.add's third parameter is an optional array to put the output into. The function can only add two arrays.

Just use the normal operators (and perhaps switch to bitwise logic operators, since you're trying to do boolean logic rather than addition):

d = a | b | c

If you want a variable number of inputs, you can use the any function:

d = np.any(inputs, axis=0)
like image 90
user2357112 supports Monica Avatar answered Dec 04 '22 00:12

user2357112 supports Monica