Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count number of trues in a numpy array in each row?

Tags:

python

numpy

I have a numpy array and I want to return the count of true values for each row.

for example I have a numpy array:

[[False False False ..., False False False]
 [False False False ..., False False False]
 [False False False ..., False  True False]
 ..., 
 [False False False ..., False False False]
 [ True False  True ...,  True  True  True]
 [False False False ..., False False False]]

and the return value should be like:

[10
 15
 8
 ..., 
 11
 10
 12]

This question asks about how to do it for the whole array but how do I do it for each row?

like image 842
aroma Avatar asked Oct 21 '25 15:10

aroma


1 Answers

We can simply do this by providing an axis argument to the sum function:

arr.sum(axis=1)
like image 186
aroma Avatar answered Oct 23 '25 05:10

aroma