布尔数组会被强制为1和0。因此,sum可以用来统计True的个数。
In [106]: arr = np.random.randn(100)
In [107]: (arr > 0).sum()
Out[107]: 52
any、all,检查数组中至少一个True或都是True。
In [108]: bools = np.array([False, False,True,False])
In [109]: bools.any()
Out[109]: True
In [110]: bools.all()
Out[110]: False
网友评论