With x
作为列堆叠版本x_0
, x_1
等等,我们可以使用np.einsum https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.einsum.html -
np.einsum('ji,jk,ki->i',x,y,x)
与混合np.einsum
and matrix-multiplcation
-
np.einsum('ij,ji->i',x.T.dot(y),x)
如前所述,x
假设是列堆叠的,如下所示:
x = np.column_stack((x_0, x_1))
运行时测试 -
In [236]: x = np.random.randint(0,255,(3,100000))
In [237]: y = np.random.randint(0,255,(3,3))
# Proposed in @titipata's post/comments under this post
In [238]: %timeit (x.T.dot(y)*x.T).sum(1)
100 loops, best of 3: 3.45 ms per loop
# Proposed earlier in this post
In [239]: %timeit np.einsum('ji,jk,ki->i',x,y,x)
1000 loops, best of 3: 832 µs per loop
# Proposed earlier in this post
In [240]: %timeit np.einsum('ij,ji->i',x.T.dot(y),x)
100 loops, best of 3: 2.6 ms per loop