老实说,没有比np.inner或np.dot更快的了。如果你觉得中间变量很烦人,你可以创建一个lambda函数:sqeuclidean = lambda x: np.inner(x, x)
np.inner和np.dot利用BLAS例程,几乎肯定比标准的元素乘法加和快。In [1]: %%timeit -n 1 -r 100 a, b = np.random.randn(2, 1000000)
((a - b) ** 2).sum()
....:
The slowest run took 36.13 times longer than the fastest. This could mean that an intermediate result is being cached
1 loops, best of 100: 6.45 ms per loop
In [2]: %%timeit -n 1 -r 100 a, b = np.random.randn(2, 1000000)
np.linalg.norm(a - b, ord=2) ** 2
....:
1 loops, best of 100: 2.74 ms per loop
In [3]: %%timeit -n 1 -r 100 a, b = np.random.randn(2, 1000000)
sqeuclidean(a - b)
....:
1 loops, best of 100: 2.64 ms per loop
np.linalg.norm(..., ord=2)在内部使用np.dot,并提供与直接使用np.inner非常相似的性能。