How to count the number of true elements in a NumP

2019-01-21 05:52发布

I have a NumPy array 'boolarr' of boolean type. I want to count the number of elements whose values are True. Is there a NumPy or Python routine dedicated for this task? Or, do I need to iterate over the elements in my script?

4条回答
我只想做你的唯一
2楼-- · 2019-01-21 06:42

In terms of comparing two numpy arrays and counting the number of matches (e.g. correct class prediction in machine learning), I found the below example for two dimensions useful:

import numpy as np
result = np.random.randint(3,size=(5,2)) # 5x2 random integer array
target = np.random.randint(3,size=(5,2)) # 5x2 random integer array

res = np.equal(result,target)
print result
print target
print np.sum(res[:,0])
print np.sum(res[:,1])

which can be extended to D dimensions.

The results are:

Prediction:

[[1 2]
 [2 0]
 [2 0]
 [1 2]
 [1 2]]

Target:

[[0 1]
 [1 0]
 [2 0]
 [0 0]
 [2 1]]

Count of correct prediction for D=1: 1

Count of correct prediction for D=2: 2

查看更多
一夜七次
3楼-- · 2019-01-21 06:48

If you wish to do a per-row count, supply axis=1 to sum:

boolarr
# array([[False, False,  True],
#        [ True, False,  True],
#        [ True, False,  True]], dtype=bool)

boolarr.sum(axis=1)
# array([1, 2, 2])

Similarly, with np.count_nonzero:

np.count_nonzero(boolarr, axis=1)
# array([1, 2, 2])
查看更多
叛逆
4楼-- · 2019-01-21 06:54

That question solved a quite similar question for me and I thought I should share :

In raw python you can use sum() to count True values in a dict :

>>> sum([True,True,True,False,False])
3

But this won't work :

>>> sum([[False, False, True], [True, False, True]])
TypeError...

Maybe this will help someone.

查看更多
放荡不羁爱自由
5楼-- · 2019-01-21 06:55

You have multiple options. Two options are the following.

numpy.sum(boolarr)
numpy.count_nonzero(boolarr)

Here's an example:

>>> import numpy as np
>>> boolarr = np.array([[0, 0, 1], [1, 0, 1], [1, 0, 1]], dtype=np.bool)
>>> boolarr
array([[False, False,  True],
       [ True, False,  True],
       [ True, False,  True]], dtype=bool)

>>> np.sum(boolarr)
5

Of course, that is a bool-specific answer. More generally, you can use numpy.count_nonzero.

>>> np.count_nonzero(boolarr)
5
查看更多
登录 后发表回答