答案 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)