np.apply_along_axis
不會真的幫到你,因爲你試圖遍歷兩個數組。實際上,您必須使用循環,如here所述。
現在,如果你的數組很小,循環就很好,但是如果N和P很大,那麼你可能想用FFT來代替。
但是,你需要適當的零墊的陣列首先,讓你的「滿」卷積有預期形狀:
M, N, P = 4, 10, 20
A = np.random.randn(M, N)
B = np.random.randn(M, P)
A_ = np.zeros((M, N+P-1), dtype=A.dtype)
A_[:, :N] = A
B_ = np.zeros((M, N+P-1), dtype=B.dtype)
B_[:, :P] = B
A_fft = np.fft.fft(A_, axis=1)
B_fft = np.fft.fft(B_, axis=1)
C_fft = A_fft * B_fft
C = np.real(np.fft.ifft(C_fft))
# Test
C_test = np.zeros((M, N+P-1))
for i in range(M):
C_test[i, :] = np.convolve(A[i, :], B[i, :], 'full')
assert np.allclose(C, C_test)
感謝您指出。然而,「權重」的論點需要是一維的。在我的情況下,它是二維的。 – Paul 2011-03-10 19:04:04
@Paul:上下文是什麼? B的權重是多少? – Benjamin 2011-03-10 19:39:47
A中的每一行都被B中的相應行過濾。我可以像這樣實現它,只是認爲可能有更快的方法。 A的大小爲10 GB,我使用overlap-add。 – Paul 2011-03-11 05:50:26