SoFunction
Updated on 2024-11-12

The handling of boolean arrays in numpy in detail

Boolean arrays can be manipulated in two main ways, any is used to see if there are any True values in the array, and all is used to see if the array is all True.

Boolean quantities are converted to 1 and 0 if they are used in a calculation, with True converted to 1 and False converted to 0. In this way it is possible to count the number of True's in an array of Boolean quantities.

If ordinary arrays are used for Boolean-like operations, there are similar data type conversions. In this case, non-zero values are converted to True, while 0 is converted to 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]: ()

Out[34]: True

In [35]: ()

Out[35]: True

In [36]: (arr > 0).all()

Out[36]: False

Above this on numpy in boolean arrays in the handling of the method of detail is what I share with you all, I hope to give you a reference, but also hope that you support me more.