numpy蒙版,用于计算满足条件的元素的数量 [英] Numpy mask to count number of elements satisfying a condition

查看:66
本文介绍了numpy蒙版,用于计算满足条件的元素的数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用Numpy将其向量化为for循环?

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

我尝试过:

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

我希望len(指数)可以计数,但事实并非如此.请提出任何建议.

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

推荐答案

获取布尔值掩码并只计算"True"数:

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

(arr1 > 0.6).sum()

这篇关于numpy蒙版,用于计算满足条件的元素的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆