布尔数组的操作方式主要有两种,any用于查看数组中是否有True的值,而all则用于查看数组是否全都是True。
如果用于计算的时候,布尔量会被转换成1和0,True转换成1,False转换成0。通过这种方法可以统计一个布尔量数组中True的个数。
如果普通的数组用于布尔类操作,也会有类似的数据类型转换。其中,非0的数值转换成True,而0则被转换成False。
In [30]: arr = randn(100)
In [31]: arr
Out[31]:
array([ 1.38474589, -1.51489066,-0.81053544, 1.47875437, -0.53638642,
0.09856211, 1.39931492,-0.04226221, -0.66064836, 0.31829036,
-0.33759781, -0.35793518, 0.66974626, 1.5989403 , 0.98361013,
0.0209635 , -0.56165749, 0.59473585, -0.06956145, -0.50384339,
-0.51207066, -0.41794862, 2.12230002, 0.55457739,-0.83156748,
-1.5609328 , -0.72414846, -0.24781724, 2.1523153 , -1.35802819,
1.75644258, 1.66794885,-0.30311682, 0.29060339, -0.18960502,
-0.91537419, -0.10277047, 0.06899507, 0.1535801 , 0.5281243 ,
-0.49951785, 0.26074368,-0.04215356, -0.29765383, -0.77197024,
0.72333408, -0.9656567 , -0.04391422, -0.53504402, -0.3695063 ,
-0.57323435, -0.09923021, -0.8819845 , -0.31904228, -0.34805511,
-1.39372713, -0.32243494, 1.18074562, -0.77189808, 0.14011272,
-0.12029721, 0.91164114, 0.3052017 , -0.45764259, 0.73858783,
0.67327449, 0.84294828, 0.54471476, 0.8300902 , -0.21001427,
-0.8247486 , 0.29870036,-0.71204709, 0.46825521, -0.76507537,
-0.67755756, 1.38798882, 0.44536155, 0.41104869, -0.24990925,
-0.38003931, 1.13801121, 0.19761371, 0.84638972, 1.05816446,
-0.03591458, 2.35862529, 1.69183501, 0.77490116, -1.47556029,
-0.54755786, -0.93202001, 0.69240349, -0.02720469, 0.49363318,
0.55501151, -1.67184849, -1.61725652, -0.95964244, 0.12177363])
In [32]: arr > 0
Out[32]:
array([ True, False, False, True, False, True, True, False, False,
True, False, False, True, True, True, True, False, True,
False, False, False, False, True, True, False, False, False,
False, True, False, True, True, False, True, False, False,
False, True, True, True, False, True, False, False,False,
True, False, False, False, False, False, False, False, False,
False, False, False, True,False, True, False, True, True,
False, True, True, True, True, True, False, False, True,
False, True, False, False, True, True, True, False, False,
True, True, True, True, False, True, True, True, False,
False, False, True, False, True, True, False, False, False, True],dtype=bool)
In [33]: (arr > 0).sum()
Out[33]: 46
In [34]: arr.any()
Out[34]: True
In [35]: arr.all()
Out[35]: True
In [36]: (arr > 0).all()
Out[36]: False
以上这篇对numpy中布尔型数组的处理方法详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。