Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count zero rows in 2D numpy array

Tags:

python

numpy

How do you count the amount of zero rows of a numpy array?

array = np.asarray([[1,1],[0,0],[1,1],[0,0],[0,0]])

-> has three rows with all zero's, hence should give 3

Took me sometime to figure out this one and also couldn't find an answer on SO

like image 953
tuuttuut Avatar asked Mar 16 '18 23:03

tuuttuut


1 Answers

You could also leverage the "truthiness" of non-zero values in an array.

np.sum(~array.any(1))

i.e., sum the rows where none of the values in said row are truthy (and hence are all zero)

like image 153
miradulo Avatar answered Sep 22 '22 01:09

miradulo