Python newbie here, I got this code from the internet(can't remember the source), and I am unable to understand how it works. What I want is to replace the output in a way so that it shows the name of the cities instead of the coordinates. Are they even linked ? Meaning once we input the values into the DB scan algorithm, do they lose their identity ? Is there any way to keep that so I can display the city names ? Any help or suggestion or edit to the question is appreciated
Here is a colab link.
kms_per_radian = 63.710088
epsilon = 1.500 / kms_per_radian
db = DBSCAN(eps=epsilon, min_samples=1, algorithm='ball_tree', metric='haversine').fit(np.radians(coords))
cluster_labels = db.labels_
num_clusters = len(set(cluster_labels))
clusters = pd.Series([coords[cluster_labels == n] for n in range(num_clusters)])
print('Number of clusters: {}'.format(num_clusters))
clustersList = clusters.tolist()
def get_centermost_point(cluster):
centroid = (MultiPoint(cluster).centroid.x, MultiPoint(cluster).centroid.y)
centermost_point = min(cluster, key=lambda point: great_circle(point, centroid).m)
return tuple(centermost_point)
lats, lons = zip(*centermost_points)
rep_points = pd.DataFrame({'lon':lons, 'lat':lats})
rs = rep_points.apply(lambda row: df[(df['lat']==row['lat']) & (df['lon']==row['lon'])].iloc[0], axis=1)
centermost_points = clusters.map(get_centermost_point)