如何创建已知距离的距离矩阵?

How to create distance matrix with known distance?

我有一个 csv。文件看起来像:

| Start| End | Distance|
|------|-----|---------|
| A    | B   |        1|
| A    | C   |        2|
| B    | C   |        3|

所需的输出是一个数字列表,如:

[[0,1,2],
 [1,0,3],
 [2,3,0]]

有人知道如何在 Python 中编码吗?

谢谢!

你提到你有一个 csv 文件,所以假设你也有一个 DataFrame:

def construct_dist_matrix(df):
    alphabet = np.unique(df[["Start", "End"]].values.ravel("K"))
    lut = {x: i for i, x in enumerate(alphabet)}
    n = len(lut)
    mat = np.zeros((n, n))

    for _, row in df.iterrows():
        s, e, d = row
        s = lut[s]
        e = lut[e]
        mat[s, e] = d
        mat[e, s] = d

    return mat, lut

测试运行:

>>> df = pd.DataFrame({
...     "Start": ["A", "A", "B"],
...     "End": ["B", "C", "C"],
...     "Distance": [1, 2, 3],
... })

>>> mat, lut = construct_dist_matrix(df)

>>> mat
array([[0., 1., 2.],
       [1., 0., 3.],
       [2., 3., 0.]])

>>> lut
{'A': 0, 'B': 1, 'C': 2}

我使用正方形找到了问题的简单答案:

import pandas as pd
from scipy.spatial.distance import squareform

df = pd.read_csv('File Name.csv')
distance_matrix = squareform(df['Distance']).tolist()
print(distance_matrix)

输出:

[[0,1,2],[1,0,3],[2,3,0]]