簡體   English   中英

查找最近坐標(lan 和 long)的每 3 個子集,Python

[英]Finding each 3 subset of closest coordinates (lan and long), Python

我有共享單車的數據集。 每個站的數據都有 lan 和 long 。 數據示例如下所示。 我想找到在坐標方面彼此接近的每 3 個站點,並總結每個子類別(3 個最近點)的計數。

我知道我們如何計算兩點之間的距離。 但我不知道如何編程,就找到最近坐標的每 3 個子集而言。

計算2點之間距離的代碼:

from math import cos, asin, sqrt, pi

def distance(lat1, lon1, lat2, lon2):
    p = pi/180
    a = 0.5 - cos((lat2-lat1)*p)/2 + cos(lat1*p) * cos(lat2*p) * (1-cos((lon2-lon1)*p))/2
    return 12742 * asin(sqrt(a)) 

數據:



    start_station_name  start_station_latitude  start_station_longitude. count
0   Schous plass                59.920259       10.760629.                 2
1   Pilestredet                 59.926224       10.729625.                 4
2   Kirkeveien                  59.933558       10.726426.                 8
3   Hans Nielsen Hauges plass   59.939244       10.774319.                 0
4   Fredensborg                 59.920995       10.750358.                 8
5   Marienlyst                  59.932454       10.721769.                 9
6   Sofienbergparken nord       59.923229       10.766171.                 3
7   Stensparken                 59.927140       10.730981.                 4
8   Vålerenga                   59.908576       10.786856.                 6
9   Schous plass trikkestopp    59.920728       10.759486.                 5
10  Griffenfeldts gate          59.933703       10.751930.                 4
11  Hallénparken                59.931530       10.762169.                 8
12  Alexander Kiellands Plass   59.928058       10.751397.                 3
13  Uranienborgparken           59.922485       10.720896.                 2
14  Sommerfrydhagen             59.911453       10.776072                  1
15  Vestkanttorvet              59.924403       10.713069.                 8
16  Bislettgata                 59.923834       10.734638                  9
17  Biskop Gunnerus' gate       59.912334       10.752292                  1
18  Botanisk Hage sør           59.915282       10.769620                  1
19  Hydroparken.                59.914145       10.715505                  1
20  Bøkkerveien                 59.927375       10.796015                  1

我想要的是:



closest                                   count_sum


Schous plass, Pilestredet, Kirkeveien.      14
.
.
.




The Error: 



---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-49-1a4d3a72c23d> in <module>
      7     for idx_1, idx_2 in [(0, 1), (1, 2), (0, 2)]:
      8         total_distance += distance(
----> 9             combination[idx_1]['start_station_latitude'],
     10             combination[idx_1]['start_station_longitude'],
     11             combination[idx_2]['start_station_latitude'],

TypeError: 'int' object is not subscriptable

您可以使用itertools.combinations()嘗試所有可能的組合,並以最短的總距離保存站點對。

from itertools import combinations

best = (float('inf'), None)

for combination in combinations(data, 3):
    total_distance = 0
    for idx_1, idx_2 in [(0, 1), (1, 2), (0, 2)]:
        total_distance += distance(
            combination[idx_1]['start_station_latitude'], 
            combination[idx_1]['start_station_longitude'], 
            combination[idx_2]['start_station_latitude'], 
            combination[idx_2]['start_station_longitude'], 
        )

    if total_distance < best[0]:
        best = (total_distance, combination)

print(f'Best combination is {best[1]}, total distance: {best[0]}')

請記住,仍有優化空間,例如緩存兩個站點之間的距離,例如

lru_cache(maxsize=None)
def distance(lat1, lon1, lat2, lon2):
    p = pi/180
    ...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM