0

在代码中,我有一个二维数组(D),对于每一列,我想提取一些相邻列(左和右)的“k”个数并将它们相加。一种天真的方法是使用 for 循环,但为了加快速度,我试图对每列的 2D 矩阵进行切片以获得子矩阵并按列求和。令人惊讶的是,幼稚的方法比使用 k > 6 的切片选项更快。关于如何使实现高效的任何建议?

天真的实现:`

k = 64
index = np.arange(D.shape[1])
index_kp = index + k 
index_kn = index - k
# neighbors can be less than k if sufficient neighbors not available; for ex. near beginning and the end of an array
index_kn[np.where(index_kn <0)] = np.where(index_kn <0)
index_kp[np.where(index_kp > (len(index)-1))] = np.where(index_kp > (len(index)-1))

Dsmear = np.empty_like(D) #stores the summation of neighboring k columns for each col
for i in range(len(index_kp)):
    Dsmear[:,i] = np.sum(D[:, index_kn[i]:index_kp[i]], axis=1)

`

切片实现:

D1 = np.concatenate((np.repeat(D[:,0].reshape(-1,1),k,axis=1), D, np.repeat(D[:,-1].reshape(-1,1),k,axis=1)),axis=1) #padding the edges with k columns 
idx = np.asarray([np.arange(i-k,i+k+1) for i in range(k, D.shape[1]+k)], dtype=np.int32)
D_broadcast = D1[:, idx] # 3D array; is a bottleneck 
Dsmear = np.sum(D_broadcast, axis=2)
4

0 回答 0