《【精编】实验报告聚类分析.pdf》由会员分享,可在线阅读,更多相关《【精编】实验报告聚类分析.pdf(20页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、1 实验报告聚类分析实验原理:K均值聚类、中心点聚类、系统聚类和EM算法聚类分析技术。实验题目:用鸢尾花的数据集,进行聚类挖掘分析。实验要求:探索鸢尾花数据的基本特征,利用不同的聚类挖掘方法,获得基本结论并简明解释。实验题目-分析报告:data(iris)rm(list=ls()gc()used(Mb)gc trigger(Mb)max used(Mb)Ncells 431730 929718 607591 Vcells 787605 8388608 1592403 data(iris)data head(data)Species1 setosa2 setosa3 setosa4 setosa
2、5 setosa6 setosa#Kmean 聚类分析 newiris newiris$Species (kc table(iris$Species,kc$cluster)1 2 3 setosa 0 50 0 versicolor 48 0 2 virginica 14 0 36 plot(newirisc(,),col=kc$cluster)points(kc$centers,c(,),col=1:3,pch=8,cex=2)#K-Mediods 进行聚类分析(cluster)library(cluster)table(iris$Species,$clustering)3 1 2 3 se
3、tosa 50 0 0 versicolor 0 3 47 virginica 0 49 1 layout(matrix(c(1,2),1,2)plot layout(matrix(1)4#hc plot(,hang=-1)plclust(,labels=FALSE,hang=-1)re sapply(unique,+function(g)iris$Species=g)1 1 setosa setosa setosa setosa setosa setosa setosa setosa setosa setosa setosa12 setosa setosa setosa setosa set
4、osa setosa setosa setosa setosa setosa setosa23 setosa setosa setosa setosa setosa setosa setosa setosa setosa setosa setosa34 setosa setosa setosa setosa setosa setosa setosa setosa setosa setosa setosa45 setosa setosa setosa setosa setosa setosaLevels:setosa versicolor virginica2 1 versicolor vers
5、icolor versicolor versicolor versicolor versicolor versicolor 8 versicolor versicolor versicolor versicolor versicolor versicolor versicolor15 versicolor versicolor versicolor versicolor versicolor versicolor versicolor22 versicolor versicolor virginica virginica virginica virginica virginica 29 vir
6、ginica virginica virginica virginica virginica virginica virginica 5 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
7、 virginica virginica virginica virginica virginica virginica 64 virginica virginica virginica virginica virginica virginica virginica 71 virginica virginica Levels:setosa versicolor virginica3 1 versicolor versicolor versicolor versicolor versicolor versicolor versicolor 8 versicolor versicolor vers
8、icolor versicolor versicolor versicolor versicolor15 versicolor versicolor versicolor versicolor versicolor versicolor versicolor22 versicolor versicolor versicolor versicolor versicolor versicolor virginica Levels:setosa versicolor virginica plot,k=4,border=light grey)#用浅灰色矩形框出4 分类聚类结果,k=3,border=d
9、ark grey)#用浅灰色矩形框出 3 分类聚类结果,k=7,which=c(2,6),border=dark grey)#DBSCAN#基于密度的聚类(fpc)library(fpc)ds1=dbscan(iris,1:4,eps=1,MinPts=5)#半径参数为 1,密度阈值为 5 ds1dbscan Pts=150 MinPts=5 eps=1 1 2border 0 1seed 50 99total 50 100 ds2=dbscan(iris,1:4,eps=4,MinPts=5)ds3=dbscan(iris,1:4,eps=4,MinPts=2)ds4=dbscan(iris
10、,1:4,eps=8,MinPts=2)par(mfcol=c(2,2)plot(ds1,iris,1:4,main=1:MinPts=5 eps=1)6 plot(ds3,iris,1:4,main=3:MinPts=2 eps=4)plot(ds2,iris,1:4,main=2:MinPts=5 eps=4)plot(ds4,iris,1:4,main=4:MinPts=2 eps=8)d=dist(iris,1:4)#计算数据集的距离矩阵d max(d);min(d)#计算数据集样本的距离的最值1 1 0(ggplot2)library(ggplot2)interval=cut_int
11、erval(d,30)table(interval)interval 0,88 585 876 891 831 688 ,543 369 379 339 335 406 ,458 459 465 480 468 505 ,349 385 321 291 187 138 ,97 92 78 50 18 4 (table(interval)7,4 for(i in 3:5)+for(j in 1:10)+ds=dbscan(iris,1:4,eps=i,MinPts=j)+print(ds)+dbscan Pts=150 MinPts=1 eps=3 1seed 150total 150dbsca
12、n Pts=150 MinPts=2 eps=3 1seed 150total 150dbscan Pts=150 MinPts=3 eps=3 1seed 150total 150dbscan Pts=150 MinPts=4 eps=3 1seed 150total 150dbscan Pts=150 MinPts=5 eps=3 1seed 150total 150dbscan Pts=150 MinPts=6 eps=3 1seed 150total 150dbscan Pts=150 MinPts=7 eps=3 1seed 150total 150dbscan Pts=150 Mi
13、nPts=8 eps=3 1seed 150total 150dbscan Pts=150 MinPts=9 eps=3 1seed 150total 1508 dbscan Pts=150 MinPts=10 eps=3 1seed 150total 150dbscan Pts=150 MinPts=1 eps=4 1seed 150total 150dbscan Pts=150 MinPts=2 eps=4 1seed 150total 150dbscan Pts=150 MinPts=3 eps=4 1seed 150total 150dbscan Pts=150 MinPts=4 ep
14、s=4 1seed 150total 150dbscan Pts=150 MinPts=5 eps=4 1seed 150total 150dbscan Pts=150 MinPts=6 eps=4 1seed 150total 150dbscan Pts=150 MinPts=7 eps=4 1seed 150total 150dbscan Pts=150 MinPts=8 eps=4 1seed 150total 150dbscan Pts=150 MinPts=9 eps=4 1seed 150total 150dbscan Pts=150 MinPts=10 eps=4 1seed 1
15、50total 1509 dbscan Pts=150 MinPts=1 eps=5 1seed 150total 150dbscan Pts=150 MinPts=2 eps=5 1seed 150total 150dbscan Pts=150 MinPts=3 eps=5 1seed 150total 150dbscan Pts=150 MinPts=4 eps=5 1seed 150total 150dbscan Pts=150 MinPts=5 eps=5 1seed 150total 150dbscan Pts=150 MinPts=6 eps=5 1seed 150total 15
16、0dbscan Pts=150 MinPts=7 eps=5 1seed 150total 150dbscan Pts=150 MinPts=8 eps=5 1seed 150total 150dbscan Pts=150 MinPts=9 eps=5 1seed 150total 150dbscan Pts=150 MinPts=10 eps=5 1seed 150total 150#30次 dbscan 的聚类结果 ds5=dbscan(iris,1:4,eps=3,MinPts=2)ds6=dbscan(iris,1:4,eps=4,MinPts=5)ds7=dbscan(iris,1:
17、4,eps=5,MinPts=9)10 par(mfcol=c(1,3)plot(ds5,iris,1:4,main=1:MinPts=2 eps=3)plot(ds6,iris,1:4,main=3:MinPts=5 eps=4)plot(ds7,iris,1:4,main=2:MinPts=9 eps=5)#EM 期望最大化聚类(mclust)library(mclust)fit_EM=Mclust(iris,1:4)fitting.|=|100%summary(fit_EM)-Gaussian finite mixture model fitted by EM algorithm-Mcl
18、ust VEV(ellipsoidal,equal shape)model with 2 components:n df BIC ICL 150 26 Clustering table:1 2 50 100 summary(fit_EM,parameters=TRUE)11-Gaussian finite mixture model fitted by EM algorithm-Mclust VEV(ellipsoidal,equal shape)model with 2 components:n df BIC ICL 150 26 Clustering table:1 2 50 100 Mi
19、xing probabilities:1 2 Means:,1 ,2Variances:,1 0.0.0.0.,2 0.0.0.0.0.0.12 plot(fit_EM)#对 EM聚类结果作图Model-based clustering plots:1:BIC2:classification3:uncertainty4:densitySelection:(下面显示选项)#选 113#选 214#选 315#选 4Selection:0 iris_BIC=mclustBIC(iris,1:4)fitting.|=|100%iris_BICsum=summary(iris_BIC,data=iri
20、s,1:4)iris_BICsum#获取数 1 据集 iris在各模型和类别数下的BIC 值Best BIC values:VEV,2 VEV,3 VVV,2BIC BIC diff Classification table for model(VEV,2):1 2 50 100 iris_BICBayesian Information Criterion(BIC):EII VII EEI VEI EVI VVI EEE1 2 3 4 16 5 6 7 8 9 EVE VEE VVE EEV VEV EVV VVV1 2 3 4 5 NA NA 6 NA 7 NA 8 9 NA Top 3 m
21、odels based on the BIC criterion:VEV,2 VEV,3 VVV,2 par(mfcol=c(1,1)17 plot(iris_BIC,G=1:7,col=yellow)mclust2Dplot(iris,1:2,+classification=iris_BICsum$classification,+parameters=iris_BICsum$parameters,col=yellow)18 iris_Dens=densityMclust(iris,1:2)#对每一个样本进行密度估计fitting.|=|100%iris_DensdensityMclust m
22、odel object:(VEV,2)Available components:1 call data modelName n 5 d G BIC bic 9 loglik df hypvol parameters 13 z classification uncertainty density plot(iris_Dens,iris,1:2,col=yellow,nlevels=55)#输入 1 或 2Model-based density estimation plots:1:BIC2:densitySelection:(下面显示选项)#选 1#选 219 Selection:0 plot(iris_Dens,type=persp,col=grey)Model-based density estimation plots:1:BIC2:densitySelection:(下面显示选项)20#选 1#选 2Selection:0