两个矩阵的列之间的交叉乘积

时间:2018-02-11 20:36:32

标签: python numpy matrix

鉴于attr_reader维度的两个矩阵enter image description hereenter image description here,我想计算enter image description here。你能建议一种矢量化方法来进行这样的计算吗?

谢谢!

1 个答案:

答案 0 :(得分:3)

这是另一种方法:

np.sum(x*y, axis=0)

效率:

x = np.random.randint(0, 10, size=(30, 400))
y = np.random.randint(0, 10, size=(30, 400))

%timeit np.sum(x*y, axis=0)
# 38.4 µs ± 942 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

%timeit for i in range(len(x.T)): x[:, i].dot(y[:, i].T)   
# 1.01 ms ± 19 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

%timeit np.diag(x.T.dot(y))
# 6.57 ms ± 248 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)