如何用NumPy计算欧几里得距离? [英] How can the Euclidean distance be calculated with NumPy?
本文介绍了如何用NumPy计算欧几里得距离?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在3D中有两点:
(xa, ya, za)
(xb, yb, zb)
我想计算距离:
dist = sqrt((xa-xb)^2 + (ya-yb)^2 + (za-zb)^2)
使用NumPy或通常使用Python的最佳方法是什么?我有:
What's the best way to do this with NumPy, or with Python in general? I have:
import numpy
a = numpy.array((xa ,ya, za))
b = numpy.array((xb, yb, zb))
推荐答案
使用 numpy.linalg.norm
:
dist = numpy.linalg.norm(a-b)
您可以在数据挖掘简介中找到背后的理论
之所以可行,是因为欧几里德距离是 l2范数,并且numpy.linalg.norm中 ord 参数的默认值为2.
This works because Euclidean distance is l2 norm and the default value of ord parameter in numpy.linalg.norm is 2.
这篇关于如何用NumPy计算欧几里得距离?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文