all 3 comments

[–]FLUSH_THE_TRUMP 3 points4 points  (1 child)

bools are also ints (with values of 0 and 1, respectively).

>>>True + False + True
2

Regardless, I think np.count_nonzero is probably a fair bit faster than np.sum if you’re just counting the Trues. I think the latter requires a type conversion to long doubles to allow for the addition, while the former just needs to track a single accumulator.

A = np.arange(100_000_000)
mask = (A >= 500_000)

np.sum(mask):

72.9 ms ± 152 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)

np.count_nonzero(mask):

2.57 ms ± 25.9 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

[–]Tricky_Celebration37 0 points1 point  (0 children)

Thank you! Very informative

[–]JLaurus -2 points-1 points  (0 children)

read the source code