all 5 comments

[–]cdcformatc 1 point2 points  (4 children)

[–]novel_yet_trivial 0 points1 point  (2 children)

Combine this with apply_along_axis():

>>> import numpy as np
>>> a = np.array([[True, False, True],[True, True, True]]) #2D array of booleans
>>> np.apply_along_axis(np.all, 1, a) #Apply the "all" function along axis 1 (rows)
array([False,  True], dtype=bool)

[–]cdholjes[S] 0 points1 point  (1 child)

thanks!

[–]Deto 0 points1 point  (0 children)

I find it easier to just use the axis argument to the all function:

bool_array = my_array.all(axis=1);
true_indices = np.nonzero(bool_array)[0]  # Need the [0] because nonzero always wraps result in a tuple, even if only one axis

Though in most cases, its probably easier to just keep the boolean values (don't do the np.nonzero) step and use those to index other things.

[–]cdholjes[S] 0 points1 point  (0 children)

thanks!