简体   繁体   English

计算每行中numpy数组中的trues数量?

[英]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. 我有一个numpy数组,我想返回每行的true值的计数。

for example I have a numpy array: 例如,我有一个numpy数组:

[[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? 这个问题询问如何为整个数组执行此操作但是如何为每一行执行此操作?

我们可以通过为sum函数提供一个axis参数来做到这一点:

arr.sum(axis=1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM