如何使用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(指数)给予计数,但事实并非如此.请给我任何建议.
如何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)