仅沿一个轴进行卷积 [英] Convolution along one axis only
本文介绍了仅沿一个轴进行卷积的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个具有相同第一轴尺寸的二维数组.在python中,我只想沿第二个轴对两个矩阵进行卷积.我想在不计算沿第一轴的卷积的情况下获得C
.
I have two 2-D arrays with the same first axis dimensions. In python, I would like to convolve the two matrices along the second axis only. I would like to get C
below without computing the convolution along the first axis as well.
import numpy as np
import scipy.signal as sg
M, N, P = 4, 10, 20
A = np.random.randn(M, N)
B = np.random.randn(M, P)
C = sg.convolve(A, B, 'full')[(2*M-1)/2]
有没有快速的方法?
推荐答案
您可以使用np.apply_along_axis
沿所需的轴应用np.convolve
.这是将Boxcar滤镜应用于2d数组的示例:
You can use np.apply_along_axis
to apply np.convolve
along the desired axis. Here is an example of applying a boxcar filter to a 2d array:
import numpy as np
a = np.arange(10)
a = np.vstack((a,a)).T
filt = np.ones(3)
np.apply_along_axis(lambda m: np.convolve(m, filt, mode='full'), axis=0, arr=a)
这是一种概括许多没有axis
参数的函数的简便方法.
This is an easy way to generalize many functions that don't have an axis
argument.
这篇关于仅沿一个轴进行卷积的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文