.
实验报告 聚类分析
实验原理:K均值聚类、中心点聚类、系统聚类和EM算法聚类分析技术。 实验题目:用鸢尾花的数据集,进行聚类挖掘分析。
实验要求:探索鸢尾花数据的基本特征,利用不同的聚类挖掘方法,获得基本结论并简明解释。
实验题目--分析报告:data(iris) > rm(list=ls()) > gc()
used (Mb) gc trigger (Mb) max used (Mb) Ncells 431730 23.1 929718 49.7 607591 32.5 Vcells 787605 6.1 8388608 64.0 1592403 12.2
> data(iris) > data<-iris > head(data)
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2 setosa 2 4.9 3.0 1.4 0.2 setosa 3 4.7 3.2 1.3 0.2 setosa 4 4.6 3.1 1.5 0.2 setosa 5 5.0 3.6 1.4 0.2 setosa 6 5.4 3.9 1.7 0.4 setosa
#Kmean聚类分析
> newiris <- iris
> newiris$Species <- NULL > (kc <- kmeans(newiris, 3))
K-means clustering with 3 clusters of sizes 62, 50, 38
Cluster means:
Sepal.Length Sepal.Width Petal.Length Petal.Width 1 5.901613 2.748387 4.393548 1.433871 2 5.006000 3.428000 1.462000 0.246000 3 6.850000 3.073684 5.742105 2.071053
Clustering vector:
[1] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
'.
.
[41] 2 2 2 2 2 2 2 2 2 2 1 1 3 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 3 1 1
[81] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 3 1 3 3 3 3 1 3 3 3 3 3 3 1 1 3 3 3 3 1
[121] 3 1 3 1 3 3 1 1 3 3 3 3 3 1 3 3 3 3 1 3 3 3 1 3 3 3 1 3 3 1
Within cluster sum of squares by cluster: [1] 39.82097 15.15100 23.87947 (between_SS / total_SS = 88.4 %)
Available components:
[1] \[6] \
> table(iris$Species, kc$cluster)
1 2 3 setosa 0 50 0 versicolor 48 0 2 virginica 14 0 36
> plot(newiris[c(\$cluster)
> points(kc$centers[,c(\ol = 1:3, pch = 8, cex=2)
#K-Mediods 进行聚类分析
'.
.
> install.packages(\ > library(cluster)
> iris.pam<-pam(iris,3)
> table(iris$Species,iris.pam$clustering)
1 2 3 setosa 50 0 0 versicolor 0 3 47 virginica 0 49 1
> layout(matrix(c(1,2),1,2)) > plot(iris.pam)
> layout(matrix(1))
'.
.
#hc
> iris.hc <- hclust( dist(iris[,1:4])) > plot( iris.hc, hang = -1)
> plclust( iris.hc, labels = FALSE, hang = -1) > re <- rect.hclust(iris.hc, k = 3) > iris.id <- cutree(iris.hc, 3)
#利用剪枝函数cutree()参数h控制输出height=18时的系谱类别 > sapply(unique(iris.id),
+ function(g)iris$Species[iris.id==g])
[[1]]
[1] setosa setosa setosa setosa setosa setosa setosa setosa setosa setosa setosa [12] setosa setosa setosa setosa setosa setosa setosa setosa setosa setosa setosa [23] setosa setosa setosa setosa setosa setosa setosa setosa setosa setosa setosa [34] setosa setosa setosa setosa setosa setosa setosa setosa setosa setosa setosa [45] setosa setosa setosa setosa setosa setosa Levels: setosa versicolor virginica [[2]]
[1] versicolor versicolor versicolor versicolor versicolor versicolor versicolor [8] versicolor versicolor versicolor versicolor versicolor versicolor versicolor [15] versicolor versicolor versicolor versicolor versicolor versicolor versicolor [22] versicolor versicolor virginica virginica virginica virginica virginica [29] virginica virginica virginica virginica virginica virginica virginica
'.
.
[36] virginica virginica virginica virginica virginica virginica virginica [43] virginica virginica virginica virginica virginica virginica virginica [50] virginica virginica virginica virginica virginica virginica virginica [57] virginica virginica virginica virginica virginica virginica virginica [64] virginica virginica virginica virginica virginica virginica virginica [71] virginica virginica
Levels: setosa versicolor virginica [[3]]
[1] versicolor versicolor versicolor versicolor versicolor versicolor versicolor [8] versicolor versicolor versicolor versicolor versicolor versicolor versicolor [15] versicolor versicolor versicolor versicolor versicolor versicolor versicolor [22] versicolor versicolor versicolor versicolor versicolor versicolor virginica Levels: setosa versicolor virginica
> plot(iris.hc)
> rect.hclust(iris.hc,k=4,border=\用浅灰色矩形框出4分类聚类结果
> rect.hclust(iris.hc,k=3,border=\用浅灰色矩形框出3分类聚类结果
> rect.hclust(iris.hc,k=7,which=c(2,6),border=\\
# DBSCAN #基于密度的聚类
> install.packages(\ > library(fpc)
> ds1=dbscan(iris[,1:4],eps=1,MinPts=5)#半径参数为1,密度阈值为5 > ds1
dbscan Pts=150 MinPts=5 eps=1 1 2 border 0 1 seed 50 99 total 50 100
> ds2=dbscan(iris[,1:4],eps=4,MinPts=5)
'.