简体   繁体   English

查找所有点对之间的欧几里得距离

[英]Finding euclidean distance between all pair of points

I have 8 points in a list and I need to calculate euclidean distance between all possible pairs. 我在列表中有8点,我需要计算所有可能的对之间的欧式距离。 I could write a for loop and go on calculating the distance but is there a better approach / method available with python / numpy / others? 我可以编写一个for循环并继续计算距离,但是python / numpy / others是否有更好的方法/方法可用?

Coordinates points: 坐标点:

x1,y1 , x2,y2, x3,y3, .... .. ,, .. ,xn,yn

Yes, you can use euclidean_distances from sklearn. 是的,你可以使用euclidean_distances从sklearn。 Usage (from the manual) 用法(来自手册)

>>> from sklearn.metrics.pairwise import euclidean_distances
>>> X = [[0, 1], [1, 1]]
>>> # distance between rows of X
>>> euclidean_distances(X, X)
array([[0., 1.],
       [1., 0.]])
>>> # get distance to origin
>>> euclidean_distances(X, [[0, 0]])
array([[1.        ],
       [1.41421356]])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM