aroma
aroma

Reputation: 1421

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

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?

Upvotes: 4

Views: 3350

Answers (1)

aroma
aroma

Reputation: 1421

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

arr.sum(axis=1)

Upvotes: 8

Related Questions