Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Doing simple operations with itertools combinatorics?

I have a python dataset that has the following structure:

cluster  pts   lon   lat

0        5      45    24
1        6      47    23
2        10     45    20

As you can see, I have a column that refers to a cluster, the number of points within a cluster, the representative latitude of the cluster and the representative longitude of the cluster. In the whole dataframe I have 140 clusters.

Now I would like to calculate for each cluster the following operation by means of a combinatorial:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

𝑤𝑒𝑖𝑔ℎ𝑡(𝑖,𝑗)=−𝑛𝑖+𝑛𝑗/𝑑𝑖𝑠𝑡(𝑖,𝑗)

where i refers to a cluster and j to another.
where n refers to the number of pts

On the one hand it does the sum of the points between cluster i and cluster j, and in the denominator it calculates by means of haversine the distance between the two clusters taking into account their representative coordinates.

I’ve started by coming up with a code that uses itertools, but I have problems to continue. Any idea?

from itertools import combinations

for c in combinations(df['cluster'],2):
    sum_pts=
    distance=
    weight=-(sum_pts/distance)
    print(c,weight)

>Solution :

As you mentioned, to do the combinations, you can use itertools.
To calculate the distance you can use geopy.distance.distance. Refer to the documentation for details: https://geopy.readthedocs.io/en/stable/#module-geopy.distance

This should work:

from itertools import combinations
from geopy.distance import distance

for p1, p2 in combinations(df['cluster'], 2):
    sum_pts = df['pts'][p1] + df['pts'][p2]
# distance in km
    dist = distance(df.loc[p1, ['lat', 'lon']], df.loc[p2, ['lat', 'lon']]).km
    weight = -sum_pts/dist
    print ((p1, p2), weight)

Edit: for a case when clusters don’t necessarily correspond to index

for c1, c2 in combinations(df['cluster'], 2):
    p1, p2 = df[df['cluster'] == c1].iloc[0], df[df['cluster'] == c2].iloc[0] 
    sum_pts = p1['pts'] + p2['pts']
    dist = distance((p1['lat'], p1['lon']), (p2['lat'], p2['lon'])).km
    weight = -sum_pts/dist
    print ((c1, c2), weight)

Output:

(0, 1) -0.04733881547464973
(0, 2) -0.033865977446857085
(1, 2) -0.04086856230889897
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading