r - 在R中的一个城市聚集长/纬度热点点的最佳方法?
问题描述
我是 R 和(无监督)机器学习的新手。我正在尝试为我在 R 中的数据找出最佳集群解决方案。
我的数据是关于什么的?
我在一个城市有一个 +/- 800 长/纬度 WGS84 坐标的数据集。
Long 在 6.90 - 6.95 范围内 lat 在 52.29 - 52.33 范围内
我想要什么?
我想根据它们的密度找到“热点”。例如:在 50 米范围内至少有 5 个长/纬点。这是一个点图示例:
我为什么要这个?
例如:假设每一点都是车祸。通过对点进行聚类,我希望看到哪些区域需要注意。(在 x 米范围内最少 x 个点需要注意)
我发现了什么?
我的解决方案似乎可以使用以下聚类算法:
- DBscan ( https://cran.r-project.org/web/packages/dbscan/dbscan.pdf )
- HDBscan(https://cran.r-project.org/web/packages/dbscan/vignettes/hdbscan.html)
- 光学(https://www.rdocumentation.org/packages/dbscan/versions/0.9-8/topics/optics)
- 城市聚类算法(https://cran.r-project.org/web/packages/osc/vignettes/paper.pdf)
我的问题
- 对于我在 R 中的情况,最好的解决方案或算法是什么?
- 我必须先将我的长/纬度转换为距离/Haversine 矩阵,这是真的吗?
解决方案
我稍微更改了这段代码,使用异常值作为发生很多事情的地方
# 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)
# 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")
不知道这是否是一个好的解决方案。但它似乎工作。也许有人有其他建议?
推荐阅读
- python - 更新新列中的值
- eclipse - 如何在一个项目中引用两个不同的元模型(如 autosar422 和 446)
- android - 如何检查来自网络的 json 是否转换为我的对象或不使用改造?
- javascript - 如何使用 JavaScript 或 Java 将 Razorpay 集成到网站中?
- javascript - 正则表达式 - 检查字符串是否包含一个或多个字符而没有一些字符
- python - PySpark:计算单词对和单个单词
- java - 将标头中的 Subject、From 和 To 值分开
- azure - IoT 中心到 Azure Edge
- excel - 在子 Worksheet_Change 中计算具有绿色背景值 = 0 的单元格的代码
- flutter - 如何在 Flutter 中使用构造函数类添加多个字符串