向量化循环Python

清除复杂

我有一个嵌套的循环:

import numpy as np

ccounter = np.zeros(shape=(120, 200))
lat_idx = np.random.randint(120, size=4800)
lon_idx = np.random.randint(200, size=(4800, 4800))
for j in range(4800):
    for i in range(4800):
        ccounter[lat_idx[i], lon_idx[i, j]] +=1 

这显然很慢。是否可以避免for循环并将其实现为例如矩阵运算?

迪卡卡

这是带有np.bincount-的矢量化方法

# Get matrix extents for output
k = lon_idx.max()+1 # 200 for given sample
n = lat_idx.max()+1 # 120 for given sample

# Get linear index equivalent
lidx = lat_idx[:,None]*k+lon_idx

# Use those indices as bins for binned count. Reshape for final o/p
out = np.bincount(lidx.ravel(),minlength=n*k).reshape(n,k)

为了进一步提高大型阵列的性能,我们可以利用numexpr以下优势lidx

import numexpr as ne

lidx = ne.evaluate('lat_idx2D*k+lon_idx',{'lat_idx2D':lat_idx[:,None]})

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章