找到所有点之间的欧氏距离

问题描述 投票:-1回答:1

我在列表中有8个点,我需要计算所有可能对之间的欧几里德距离。我可以编写一个for循环并继续计算距离但是python / numpy / others有更好的方法/方法吗?

坐标点:

x1,y1 , x2,y2, x3,y3, .... .. ,, .. ,xn,yn
python algorithm numpy euclidean-distance
1个回答
1
投票

是的,你可以使用sklearn的euclidean_distances。用法(来自手册)

>>> 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]])
© www.soinside.com 2019 - 2024. All rights reserved.