在 DB 扫描算法中显示名称而不是坐标
Display names instead of coordinates in DB scan algorithm
Python 新手,我从网上得到了这个代码(不记得出处了),我无法理解它是如何工作的。我想要的是以某种方式替换输出,以便它显示城市名称而不是坐标。他们甚至有联系吗?这意味着一旦我们将值输入到数据库扫描算法中,它们会丢失它们的身份吗?有没有办法保留它以便我可以显示城市名称? 感谢对问题的任何帮助或建议或编辑
这里是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)
clusters1 = pd.Series([names[cluster_labels == n] for n in range(num_clusters)])
clusters = pd.Series([coords[cluster_labels == n] for n in range(num_clusters)])
print(clusters1)
print(clusters)
print(df)
我查看了你的代码,我发现这个簇坐标是根据标签分组的。我没有看到那里的 clusters1,而是根据坐标对集群名称进行了分组。希望我能回答你的问题。
Python 新手,我从网上得到了这个代码(不记得出处了),我无法理解它是如何工作的。我想要的是以某种方式替换输出,以便它显示城市名称而不是坐标。他们甚至有联系吗?这意味着一旦我们将值输入到数据库扫描算法中,它们会丢失它们的身份吗?有没有办法保留它以便我可以显示城市名称? 感谢对问题的任何帮助或建议或编辑
这里是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)
clusters1 = pd.Series([names[cluster_labels == n] for n in range(num_clusters)])
clusters = pd.Series([coords[cluster_labels == n] for n in range(num_clusters)])
print(clusters1)
print(clusters)
print(df)
我查看了你的代码,我发现这个簇坐标是根据标签分组的。我没有看到那里的 clusters1,而是根据坐标对集群名称进行了分组。希望我能回答你的问题。