对 2D numpy 数组的每一列进行切片

问题描述

代码中,我有一个二维数组(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,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)

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)