小编Sur*_*bra的帖子

Numpy掩码计算满足条件的元素数量

如何使用Numpy对此for循环进行向量化?

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
Run Code Online (Sandbox Code Playgroud)

我试过了:

count=0
arr1 = np.random.rand(184,184)
mask = (arr1>0.6)
indices = np.where(mask)
print indices , len(indices) 
Run Code Online (Sandbox Code Playgroud)

我期望len(指数)给予计数,但事实并非如此.请给我任何建议.

python arrays numpy vectorization

7
推荐指数
2
解决办法
6412
查看次数

Numpy矢量化

如何numpy vectorize以下python代码(for循环)?任何帮助都感激不尽

arr1 = np.ndarray(shape = (184,184))
arr2 = np.ndarray(shape = (184,184))
arr3 = np.full((184, 184), 0.0, dtype=float)
for i in range(arr1.size[0]):
    for j in range(arr1.size[1]):
        if arr2[i,j] == 0 or arr1[i,j] == 0:
            arr3[i,j]=0
        elif arr2[i,j] == 255 and arr1[i,j] == 255:
            arr3[i, j] = 255
Run Code Online (Sandbox Code Playgroud)

python numpy vectorization

3
推荐指数
1
解决办法
60
查看次数

标签 统计

numpy ×2

python ×2

vectorization ×2

arrays ×1