How to count the number of true elements in a NumPy bool array

后端 未结 4 939
迷失自我
迷失自我 2020-12-12 13:50

I have a NumPy array \'boolarr\' of boolean type. I want to count the number of elements whose values are True. Is there a NumPy or Python routine dedicated for

4条回答
  •  甜味超标
    2020-12-12 13:57

    boolarr.sum(axis=1 or axis=0)
    

    axis = 1 will output number of trues in a row and axis = 0 will count number of trues in columns so

    boolarr[[true,true,true],[false,false,true]]
    print(boolarr.sum(axis=1))
    

    will be (3,1)

提交回复
热议问题