Home  >  Article  >  Backend Development  >  How to deal with boolean arrays in numpy

How to deal with boolean arrays in numpy

不言
不言Original
2018-04-17 11:11:042645browse

The following is a detailed explanation of the processing method of Boolean arrays in numpy. It has a good reference value and I hope it will be helpful to everyone. Let's take a look together

There are two main ways to operate Boolean arrays. any is used to check whether there is a True value in the array, and all is used to check whether the array is all True.

If used for calculation, the Boolean quantity will be converted to 1 and 0, True will be converted to 1, and False will be converted to 0. This method can count the number of True in a Boolean array.

If ordinary arrays are used for Boolean operations, there will be similar data type conversions. Among them, non-zero values ​​are converted to True, and 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.6 6064836, 0.31829036,

-0.33759781, -0.35793518, 0.66974626, 1.5989403, 0.98361013,

0.0209635, -0.56165749, 0.59473585, -0.06956145, -0.5038 4339,

-0.51207066, -0.41794862, 2.12230002, 0.55457739 . 29060339, -0.18960502,

-0.91537419 . 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.1 4011272,

-0.12029721, 0.91164114 0.3052017 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.0581644 6,

-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.12177 363])

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

Related recommendations:

Numpy masked array detailed explanation

The above is the detailed content of How to deal with boolean arrays in numpy. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn