How can the Euclidean distance be calculated with NumPy?

后端 未结 22 934
春和景丽
春和景丽 2020-11-22 02:29

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 + (         


        
22条回答
  •  忘了有多久
    2020-11-22 02:43

    Use numpy.linalg.norm:

    dist = numpy.linalg.norm(a-b)
    

    You can find the theory behind this in Introduction to Data Mining

    This works because the Euclidean distance is the l2 norm, and the default value of the ord parameter in numpy.linalg.norm is 2.

提交回复
热议问题