考虑以下代码,该代码从标记坐标的列表生成距离矩阵:
import numpy as np
import pandas as pd
from scipy.spatial.distance import pdist, squareform
coord_data = [
[1, 2],
[4, 3],
[5, 8],
[6, 7],
]
df = pd.DataFrame(coord_data, index=list('ABCD'))
dist_matrix = squareform(pdist(df, metric='euclidean'))
dist_df = pd.DataFrame(dist_matrix, index=df.index, columns=df.index)
print(dist_df)
A B C D
A 0.000000 3.162278 7.211103 7.071068
B 3.162278 0.000000 5.099020 4.472136
C 7.211103 5.099020 0.000000 1.414214
D 7.071068 4.472136 1.414214 0.000000
有没有一种高效的方法(使用numpy,pandas等)从这个距离矩阵中获取N个最小距离对?
例如,如果N=2,则希望得到类似于以下示例的输出:
[['C', 'D'], ['A', 'B']] # corresponding to minimum distances [1.414214, 3.162278]