Numpy mask to count number of elements satisfying

2020-03-19 02:01发布

How to use Numpy to vectorize this for loop?

count=0
arr1 = np.random.rand(184,184)
for i in range(arr1.size[0]):
    for j in range(arr1.size[1]):
        if arr1[i,j] > 0.6:
            count += 1
print count

I tried:

count=0
arr1 = np.random.rand(184,184)
mask = (arr1>0.6)
indices = np.where(mask)
print indices , len(indices) 

I expected len(indices) to give count, but it didn't. Any suggestions please.

3条回答
走好不送
2楼-- · 2020-03-19 02:22

You can also use size attribute of Numpy array:

arr1 = np.random.rand(184,184)

arr1[ arr1 > 0.6 ].size
查看更多
Summer. ? 凉城
3楼-- · 2020-03-19 02:27

np.count_nonzero should be a bit faster than the sum:

np.count_nonzero(arr1 > 0.6)

In fact, it is three times as fast

>>> from timeit import repeat
>>> kwds = dict(globals=globals(), number=10000)
>>> 
>>> arr1 = np.random.rand(184,184)
>>> 
>>> repeat('np.count_nonzero(arr1 > 0.6)', **kwds)
[0.15281831508036703, 0.1485864429268986, 0.1477385900216177]
>>> repeat('(arr1 > 0.6).sum()', **kwds)
[0.5286932559683919, 0.5260644309455529, 0.5260107989888638]
查看更多
等我变得足够好
4楼-- · 2020-03-19 02:32

Get a boolean mask and just count the "True"s:

(arr1 > 0.6).sum()
查看更多
登录 后发表回答