创建距离矩阵? [英] Creating a Distance Matrix?
问题描述
我目前正在将数据读入如下所示的数据框中.
I am currently reading in data into a dataframe that looks like this.
City XCord YCord
Boston 5 2
Phoenix 7 3
New York 8 1
..... . .
我想根据此数据创建一个欧几里得距离矩阵,该矩阵显示所有城市对之间的距离,因此我得到一个结果矩阵,如:
I want to to create a Euclidean Distance Matrix from this data showing the distance between all city pairs so I get a resulting matrix like:
Boston Phoenix New York
Boston 0 2.236 3.162
Phoenix 2.236 0 2.236
New York 3.162 2.236 0
我的实际数据框中还有更多城市和坐标,因此我需要能够以某种方式迭代所有城市对,并创建一个距离矩阵,就像上面显示的那样,但我不确定如何将所有引用配对在一起并应用欧几里德距离公式?任何帮助将不胜感激.
There are many more cities and coordinates in my actual data frame so i need to to be able to somehow iterate over all of the city pairs and create a distance matrix like the one I have shown above but I am not sure how to pair all of the cites together and apply the Euclidean Distance formula? Any help would be appreciated.
推荐答案
我认为您对例如:
创建数据:
import pandas as pd
from scipy.spatial import distance_matrix
data = [[5, 7], [7, 3], [8, 1]]
ctys = ['Boston', 'Phoenix', 'New York']
df = pd.DataFrame(data, columns=['xcord', 'ycord'], index=ctys)
输出:
xcord ycord
Boston 5 7
Phoenix 7 3
New York 8 1
使用距离矩阵函数:
pd.DataFrame(distance_matrix(df.values, df.values), index=df.index, columns=df.index)
结果:
Boston Phoenix New York
Boston 0.000000 4.472136 6.708204
Phoenix 4.472136 0.000000 2.236068
New York 6.708204 2.236068 0.000000
这篇关于创建距离矩阵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!