使用数组作为索引沿轴应用

apply along axis using arrays as indicies

我正在尝试在没有循环的情况下就地执行此功能:

for i in xrange(2):
    trend[i] = np.convolve(dat[i,0], aW3[:,i], 'same').sum()

我最好的尝试如下:

trend[:2] = np.apply_along_axis(
    func1d=lambda x: np.convolve(x, aW3[:,i], 'same').sum(),
    axis=1,
    arr=dat[:2,0])

但我不知道如何通过 func1d

正确索引 aW3[:,i]

使用的常量

aW3 = np.array( [[ 0.259,  0.407],
                 [ 0.37 ,  0.407],
                 [ 0.259,  0.185],
                 [ 0.111,  0.   ]])
dat = np.array([0.02360784,  0.0227628 ,  0.0386366 ,  0.03338596,  0.03141621, 0.03430469])
dat = dat.reshape(dat.shape[0], 1) # in columns
trend = np.fromiter((np.convolve(dat[i,0], aW3[:,i], 'same').sum() for i in xrange(2)), float)

看来你可以只使用 np.einsum 作为 vectorized 的解决方案,就像这样 -

trend = np.einsum('i,ji->i',dat[0:aW3.shape[1],0],aW3)

或者用 broadcasting -

trend = (dat[0:aW3.shape[1],0]*aW3).sum(0)