距离矩阵的坐标列表

时间:2015-06-08 14:52:24

标签: python numpy matrix distance

我有一个列为8个坐标列表的列表:

coordinates = [[47.2486, -1.54806],
               [43.5656, 1.47417],
               [48.3592, -4.57],
               [48.1439, 17.1097],
               [39.6275, 140.198],
               [30.0458, 31.2625],
               [38.9371, -77.0869],
               [33.9, 35.4823]]

使用以下脚本,我寻求输出坐标矩阵:

import numpy
from scipy.spatial.distance import pdist

coordinates_array = numpy.array(coordinates)
dist_array = pdist(coordinates_array)

dist_matrix = numpy.reshape(dist_array, newshape=(len(coordinates), len(coordinates)))

但是,我得到ValueError: total size of new array must be unchanged。那是为什么?

1 个答案:

答案 0 :(得分:2)

如scipy文档中所述,使用scipy.spatial.distance.squareformpdist返回的压缩距离矩阵转换为方形距离矩阵,

from scipy.spatial.distance import squareform 

dist_matrix = squareform(dist_array)