How can the euclidean distance be calculated with numpy?
I have two points in 3D:
(xa, ya, za)
(xb, yb, zb)
And I want to calculate the distance:
dist = sqrt((xa-xb)^2 + (ya-yb)^2 + (za-zb)^2)
What's the best way to do this with Numpy, or with Python in general? I have:
a = numpy.array((xa ,ya, za))
b = numpy.array((xb, yb, zb))
使用numpy.linalg.norm
:
dist = numpy.linalg.norm(a-b)
There's a function for that in SciPy, it's called Euclidean
example:
from scipy.spatial import distance
a = (1,2,3)
b = (4,5,6)
dst = distance.euclidean(a,b)
Another instance of this problem solving method. As soon as I submitted the question I got it:
def dist(x,y):
return numpy.sqrt(numpy.sum((x-y)**2))
a = numpy.array((xa,ya,za))
b = numpy.array((xb,yb,zb))
dist_a_b = dist(a,b)
链接地址: http://www.djcxy.com/p/50980.html
上一篇: 如何将csv读入numpy的记录数组?
下一篇: 欧几里德距离如何用numpy来计算?