【问题标题】:R: How to add the noise cluster into DBSCAN plotR:如何将噪声簇添加到 DBSCAN 图中
【发布时间】:2015-11-01 09:44:30
【问题描述】:

我正在尝试绘制 DBSCAN 结果。这是我到目前为止所做的。我的距离矩阵是here

dbs55_CR_EUCL = dbscan(writeCRToMatrix,eps=0.006, MinPts = 4, method = "dist")

plot(writeCRToMatrix[dbs55_CR_EUCL$cluster>0,], 
     col=dbs55_CR_EUCL$cluster[dbs55_CR_EUCL$cluster>0],
     main="DBSCAN Clustering K = 4 \n (EPS=0.006, MinPts=4) without noise",
     pch = 20)

剧情如下:

当我尝试绘制包括噪声簇在内的所有簇时,我只能在绘图中看到 2 个点。

我正在寻找的是

  1. 将噪声簇中的点添加到图中,但符号不同。类似于下图的东西

  1. 如下图所示对集群区域进行着色

【问题讨论】:

    标签: r plot cluster-analysis dbscan


    【解决方案1】:

    噪声簇的 id 为 0。R 图通常会忽略颜色 0,因此如果要显示噪声点(显示为黑色),则需要执行以下操作:

    plot(writeCRToMatrix, 
      col=dbs55_CR_EUCL$cluster+1L,
      main="DBSCAN Clustering K = 4 \n (EPS=0.006, MinPts=4) with noise",
      pch = 20)
    

    如果您想要一个不同的噪音符号,那么您可以执行以下操作(改编自手册页):

    library(dbscan)
    n <- 100
    x <- cbind(
         x = runif(10, 0, 10) + rnorm(n, sd = 0.2),
         y = runif(10, 0, 10) + rnorm(n, sd = 0.2)
    )
    
    res <- dbscan::dbscan(x, eps = .2, minPts = 4)
    plot(x, col=res$cluster, pch = 20)
    points(x[res$cluster == 0L], col = "grey", pch = "+")
    

    这里的代码将为每个集群创建一个阴影凸包

    library(ggplot2)
    library(data.table)
    library(dbscan)
    
    
    dt <- data.table(x, level=as.factor(res$cluster), key = "level")
    hulls <- dt[, .SD[chull(x, y)], by = level]
    
    ### get rid of hull for noise
    hulls <- hulls[level != "0",]
    
    cols <- c("0" = "grey", "1" = "red", "2" = "blue")
    
    ggplot(dt, aes(x=x, y=y, color=level)) +
      geom_point() +
      geom_polygon(data = hulls, aes(fill = level, group = level),
        alpha = 0.2, color = NA) +
      scale_color_manual(values = cols) +
      scale_fill_manual(values = cols)
    

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 2021-07-06
      • 2015-07-01
      • 2017-08-27
      • 2021-07-04
      • 2021-01-12
      • 2019-04-04
      • 2020-06-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多