如何计算数组中每对点之间的总欧几里得距离

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

我有一个如下数组:

array([[-1.53172534,  0.47023084],
       [-1.45365077,  0.47860466],
       [-1.77932397,  0.63310581],
       ...,
       [-1.30975015,  1.29030593],
       [-0.94061512,  0.98730601],
       [-1.54057471,  1.24052875]])

我想为K-Means差异度量计算每对点之间的总欧几里得距离。 (该数组的大小为n x 2,大小为3000> n> 500)。

python k-means
1个回答
0
投票

您可以自己计算而无需任何模块的帮助。

import math

locations = [
    [-1.53172534,  0.47023084],
    [-1.45365077,  0.47860466],
    [-1.77932397,  0.63310581],
    ...
    [-1.30975015,  1.29030593],
    [-0.94061512,  0.98730601],
    [-1.54057471,  1.24052875]
]

# This is the resultant matrix containing distances from each point to each point
dist_matrix = []
for starting_point in locations:
    distances = []
    for ending_point in locations:
        distances.append(math.sqrt(sum([(a - b) ** 2 for a, b in zip(starting_point, ending_point)])))
    dist_matrix.append(distances)

查看此tutorial以获取更多说明。

© www.soinside.com 2019 - 2024. All rights reserved.