首页 > 解决方案 > 在R中的一个城市聚集长/纬度热点点的最佳方法?

问题描述

我是 R 和(无监督)机器学习的新手。我正在尝试为我在 R 中的数据找出最佳集群解决方案。

我的数据是关于什么的?

我在一个城市有一个 +/- 800 长/纬度 WGS84 坐标的数据集。

Long 在 6.90 - 6.95 范围内 lat 在 52.29 - 52.33 范围内

我想要什么?

我想根据它们的密度找到“热点”。例如:在 50 米范围内至少有 5 个长/纬点。这是一个点图示例:

点图

我为什么要这个?

例如:假设每一点都是车祸。通过对点进行聚类,我希望看到哪些区域需要注意。(在 x 米范围内最少 x 个点需要注意)

我发现了什么?

我的解决方案似乎可以使用以下聚类算法:

  1. DBscan ( https://cran.r-project.org/web/packages/dbscan/dbscan.pdf )
  2. HDBscan(https://cran.r-project.org/web/packages/dbscan/vignettes/hdbscan.html
  3. 光学(https://www.rdocumentation.org/packages/dbscan/versions/0.9-8/topics/optics
  4. 城市聚类算法(https://cran.r-project.org/web/packages/osc/vignettes/paper.pdf

我的问题

  1. 对于我在 R 中的情况,最好的解决方案或算法是什么?
  2. 我必须先将我的长/纬度转换为距离/Haversine 矩阵,这是真的吗?

标签: ralgorithmcluster-analysishierarchical-clusteringunsupervised-learning

解决方案


查找感兴趣的内容:https ://gis.stackexchange.com/questions/64392/finding-clusters-of-points-based-distance-rule-using-r

我稍微更改了这段代码,使用异常值作为发生很多事情的地方

# 1. Make spatialpointsdataframe #

xy <- SpatialPointsDataFrame(
  matrix(c(x,y), ncol=2), data.frame(ID=seq(1:length(x))),
  proj4string=CRS("+proj=longlat +ellps=WGS84 +datum=WGS84"))

# 2. Use DISTM function to generate distance matrix.# 
mdist <- distm(xy)

# 3. Use hierarchical clustering with complete methode#
hc <- hclust(as.dist(mdist), method="complete")

# 4. Show dendogram#
plot(hc, labels = input$street, xlab="", sub="",cex=0.7)

# 5. Set distance: in my case 300 meter#
d=300

# 6. define clusters based on a tree "height" cutoff "d" and add them to the SpDataFrame
xy$clust <- cutree(hc, h=d)

# 7. Add clusters to dataset#
input$cluster <- xy@data[["clust"]]

# 8. Plot clusters #
plot(input$long, input$lat, col=input$cluster, pch=20)
text(input$long, input$lat, labels =input$cluster)

300米集群

# 9. Count n in cluster#
selection2 <- input %>% count(cluster)

# 10. Make a boxplot #
boxplot(selection2$n)

#11. Get first outlier#
outlier <- boxplot.stats(selection2$n)$out
outlier <- sort(outlier)
outlier <- as.numeric(outlier[1])

#12. Filter clusters greater than outlier#
selectie3 <- as.vector(selection2 %>% filter(selection2$n >= outlier[1]) %>% select(cluster))

#13. Make a new DF with all outlier clusters#
heatclusters <- input %>% filter(cluster%in% c(selectie3$cluster))

#14. Plot outlier clusters#
plot(heatclusters$long, heatclusters$lat, col=heatclusters$cluster)

离群值集群

#15. Plot on density map ##
googlemap + geom_point(aes(x=long , y=lat), data=heatclusters, color="red", size=0.1, shape=".") +
  stat_density2d(data=heatclusters,
                 aes(x =long, y =lat, fill= ..level..), alpha = .2, size = 0.1,
                 bins = 10, geom = "polygon") + scale_fill_gradient(low = "green", high = "red") 

不知道这是否是一个好的解决方案。但它似乎工作。也许有人有其他建议?


推荐阅读