实例一、对鸢尾花数据进行DBSCAN聚类分类
data(iris)
library(fpc)
model_dbscan <- dbscan(iris[, 1:4], 1.5, 5, scale=T, showplot = T, method = "raw")
![]()
![]()
![]()
![]()
![]()
![]()
![]()
model_dbscan <- dbscan(iris[, 1:4], 0.42, 5, showplot = T, method = "raw")
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
![]()
str(model_dbscan)
## List of 4
## $ cluster: num [1:150] 1 1 1 1 1 1 1 1 1 1 ...
## $ eps : num 0.42
## $ MinPts : num 5
## $ isseed : logi [1:150] TRUE TRUE TRUE TRUE TRUE TRUE ...
## - attr(*, "class")= chr "dbscan"
table(model_dbscan$cluster, iris[, 5])
##
## setosa versicolor virginica
## 0 2 10 17
## 1 48 0 0
## 2 0 37 0
## 3 0 3 33
plotcluster(iris[, 1:4], model_dbscan$cluster)
![]()
实例二、k-means与DBSCAN聚合非球形簇
x1 <- seq(0, pi, length.out = 100)
y1 <- sin(x1) + 0.1*rnorm(100)
x2 <- 1.5 + seq(0, pi, length.out = 100)
y2 <- cos(x2) + 0.1*rnorm(100)
data <- data.frame(c(x1, x2), c(y1, y2))
names(data) <- c("x", "y")
model_kmeans <- kmeans(data, centers = 2, nstart = 10)
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 3.2.3
p <- ggplot(data, aes(x, y))
p + geom_point(size=2.5, aes(colour=factor(model_kmeans$cluster))) + theme(legend.position="top")
![]()
model_dbscan <- dbscan(data, eps = 0.6, MinPts = 4)
p + geom_point(size=2.5, aes(colour=factor(model_dbscan$cluster))) + theme(legend.position="top")
![]()