您好,登錄后才能下訂單哦!
布爾數(shù)組的操作方式主要有兩種,any用于查看數(shù)組中是否有True的值,而all則用于查看數(shù)組是否全都是True。
如果用于計算的時候,布爾量會被轉(zhuǎn)換成1和0,True轉(zhuǎn)換成1,F(xiàn)alse轉(zhuǎn)換成0。通過這種方法可以統(tǒng)計一個布爾量數(shù)組中True的個數(shù)。
如果普通的數(shù)組用于布爾類操作,也會有類似的數(shù)據(jù)類型轉(zhuǎn)換。其中,非0的數(shù)值轉(zhuǎn)換成True,而0則被轉(zhuǎn)換成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中布爾型數(shù)組的處理方法詳解就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。