用二维数组卷积高维立方体

时间:2019-05-16 08:06:56

标签: python arrays numpy matrix

我正在对形状为[nLambda,nX,nY]的3D数据立方体与形状为[nLambda,3]的滤镜进行卷积。我设法使这种方法适用于这种情况,但是我需要对保存为[nt,nLambda,nX,nY]的大量多维数据集重复此过程。我想扩展必须处理的代码,但是我一直在弄乱这些内容。有谁知道该怎么做?

我当前的程序可以:

datacube.shape         = [21,100,100]
filters.shape          = [21,3]
data_collapsed.shape   = [100,100,3]   

我要它做

datacube.shape         = [10,21,100,100]
filters.shape          = [21,3]
data_collapsed.shape   = [10,100,100,3]     

我用于3D多维数据集的代码

nl,nx,ny = datacube.shape
filter_rgb = np.tile(filters, (ny,nx,1,1))
filter_rgb = np.swapaxes(filter_rgb, 0,2)
data_rgb = np.tile(datacube,(3,1,1,1))
data_rgb = np.swapaxes(data_rgb,0,-1)
data_filtered = data_rgb * filter_rgb
data_collapsed = np.sum(data_filtered, axis=0)

1 个答案:

答案 0 :(得分:1)

您可以使用np.tensordot

对于datacube的情况,4D-

data_collapsed = np.tensordot(datacube,filters,axes=(1,0))

对于3D情况-

data_collapsed = np.tensordot(datacube,filters,axes=(0,0))

Related post to understand tensordot