【问题标题】:Distance Matrix Haversine距离矩阵半正弦
【发布时间】:2022-07-19 01:56:37
【问题描述】:

我正在处理如下所示的数据框:

             lat       lon
id_zone
0        40.0795  4.338600
1        45.9990  4.829600
2        45.2729  2.882000
3        45.7336  4.850478
4        45.6981  5.043200

我正在尝试制作 Haverisne 距离矩阵。基本上对于每个区域,我想计算它与数据框中所有其他区域之间的距离。所以对角线上应该只有 0。这是我使用的 Haversine 函数,但我无法制作矩阵。

def haversine(x):
    x.lon, x.lat, x.lon2, x.lat2 = map(radians, [x.lon, x.lat, x.lon2, x.lat2])
    # formule de Haversine
    dlon = x.lon2 - x.lon
    dlat = x.lat2 - x.lat
    a = sin(dlat / 2) ** 2 + cos(x.lat) * cos(x.lat2) * sin(dlon / 2) ** 2
    c = 2 * atan2(sqrt(a), sqrt(1 - a))
    km = 6367 * c
    return km

【问题讨论】:

  • 在 StackOverflow 上,您不应该要求一个完整的解决方案。尝试解决您的任务并询问您遇到的特定问题。 “我无法制作我的矩阵。”没有充分描述您的问题。显示您的相关代码(作为文本,而不是图片),描述您期望它做什么以及实际发生的情况。

标签: python pandas dataframe matrix haversine


【解决方案1】:

你可以使用这个答案的解决方案Pandas - Creating Difference Matrix from Data Frame

或者在你的特定情况下,你有一个像这个例子这样的 DataFrame:

             lat       lon
id_zone
0        40.0795  4.338600
1        45.9990  4.829600
2        45.2729  2.882000
3        45.7336  4.850478
4        45.6981  5.043200

你的函数定义为:

def haversine(first, second):
    # convert decimal degrees to radians
    lat, lon, lat2, lon2 = map(np.radians, [first[0], first[1], second[0], second[1]])

    # haversine formula
    dlon = lon2 - lon
    dlat = lat2 - lat
    a = np.sin(dlat/2)**2 + np.cos(lat) * np.cos(lat2) * np.sin(dlon/2)**2
    c = 2 * np.arcsin(np.sqrt(a))
    r = 6371 # Radius of earth in kilometers. Use 3956 for miles
    return c * r

您通过first 位置和second 位置的latlon 的位置。

然后您可以使用 Numpy 创建一个距离矩阵,然后将零点替换为 hasrsine 函数的距离结果:

# create a matrix for the distances between each pair of zones
distances = np.zeros((len(df), len(df)))
for i in range(len(df)):
    for j in range(len(df)):
        distances[i, j] = haversine(df.iloc[i], df.iloc[j])
pd.DataFrame(distances, index=df.index, columns=df.index)

你的输出应该是这样的:

id_zone           0           1           2           3           4
id_zone
0          0.000000  659.422944  589.599339  630.083979  627.383858
1        659.422944    0.000000  171.597296   29.555376   37.325316
2        589.599339  171.597296    0.000000  161.731366  174.983855
3        630.083979   29.555376  161.731366    0.000000   15.474533
4        627.383858   37.325316  174.983855   15.474533    0.000000

【讨论】:

    猜你喜欢
    • 2018-09-20
    • 2016-08-23
    • 2018-09-15
    • 1970-01-01
    • 2016-11-29
    • 2015-07-14
    • 1970-01-01
    • 2016-12-06
    • 2011-08-08
    相关资源
    最近更新 更多