【问题标题】:Clustering algorithm for mapping映射的聚类算法
【发布时间】:2016-11-09 10:50:22
【问题描述】:
我在地图上有很多点(经度/纬度),我想将它们聚集到组中,
类似 k-means。
有什么方法可以插入点(经度/纬度)和组数,并为每个点获取她属于哪个组?
【问题讨论】:
标签:
r
google-maps
gis
cluster-analysis
k-means
【解决方案1】:
您的问题本质上是“我如何进行聚类”。
无法简明扼要地回答,但你应该阅读关于聚类的教科书。
k-means 不适用于经纬度,因为它是最小二乘法,但 1 度纬度不等于 1 度经度(通常)。使用 PAM、层次聚类、DBSCAN 等。
【解决方案2】:
library(ggplot2)
# Construct Data
set.seed(23)
lat <- c(seq(from = 16.3478, to = 14.1329876, length.out = 500),
seq(from = 18.5478, to = 19.567, length.out = 500))
lat <- sample(x = lat, size = 100)
lon <- seq(from = 45.987, to = 46.98237, length.out = 1000)
lon <- sample(x = lon, size = 100)
# Place inside data.frame
df_latlon <- data.frame(lat, lon)
cluster_latlon <- kmeans(x = df_latlon, centers = 2, nstart = 20)
df_latlon <- cbind(df_latlon, cluster_latlon$cluster)
# Output ggplot with colored values
ggplot(df_latlon) +
geom_point(aes(lat, lon, color = as.factor(cluster_latlon$cluster)))
cluster_latlon$centers
cluster_latlon$cluster