本文主要是介绍Python-Numpy-计算向量间的欧式距离,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
两个向量间的欧式距离公式:
a = np.array([[2, 2], [4, 5], [6, 7]])
b = np.array([[1, 1]])
# 使用L2范数计算
dev1 = np.linalg.norm(a - b, ord=2, axis=1)
# 使用公式计算
dev2 = np.sqrt(np.sum((a - b) ** 2, axis=1))
print(dev1.reshape((-1, 1)), dev2.reshape((-1, 1)))
# [[1.41421356], [5. ], [7.81024968]]
# [[1.41421356], [5. ], [7.81024968]]
这篇关于Python-Numpy-计算向量间的欧式距离的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!