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

我有一个像下面这样的数组:

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均值相异性度量计算每对点之间的总欧几里得距离。 (该数组的大小为n x 2,大小为3000> n> 500)。

评论
  • 猪猪侠
    猪猪侠 回复

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

    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)
    

    Check out this tutorial for further explanation.