一个用R语言进行Kmeans聚类分析的例子.docx

上传人:b****3 文档编号:3454486 上传时间:2022-11-23 格式:DOCX 页数:4 大小:16.58KB
下载 相关 举报
一个用R语言进行Kmeans聚类分析的例子.docx_第1页
第1页 / 共4页
一个用R语言进行Kmeans聚类分析的例子.docx_第2页
第2页 / 共4页
一个用R语言进行Kmeans聚类分析的例子.docx_第3页
第3页 / 共4页
一个用R语言进行Kmeans聚类分析的例子.docx_第4页
第4页 / 共4页
亲,该文档总共4页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

一个用R语言进行Kmeans聚类分析的例子.docx

《一个用R语言进行Kmeans聚类分析的例子.docx》由会员分享,可在线阅读,更多相关《一个用R语言进行Kmeans聚类分析的例子.docx(4页珍藏版)》请在冰豆网上搜索。

一个用R语言进行Kmeans聚类分析的例子.docx

一个用R语言进行Kmeans聚类分析的例子

一个用R语言进行Kmeans聚类分析的例子

在网上()找到了一个用R语言进行聚类分析的例子,在整个例子中做了一些中文解释说明.数据集用的是iris第一步:

对数据集进行初步统计分析

检查数据的维度

>dim(iris)

[1]1505显示数据集中的列名

>names(iris)

[1]"Sepal.Length""Sepal.Width""Petal.Length""Petal.Width""Species"显示数据集的内部结构

>str(iris)

'data.frame':

150obs.of5variables:

$Sepal.Length:

num5.14.94.74.655.44.654.44.9...

$Sepal.Width:

num3.533.23.13.63.93.43.42.93.1...

$Petal.Length:

num1.41.41.31.51.41.71.41.51.41.5...

$Petal.Width:

num0.20.20.20.20.20.40.30.20.20.1...

$Species:

Factorw/3levels"setosa","versicolor",..:

1111111111...显示数据集的属性

>attributes(iris)

$names--就是数据集的列名

[1]"Sepal.Length""Sepal.Width""Petal.Length""Petal.Width""Species"$row.names--个人理解就是每行数据的标号

[1]1234567891011121314151617181920

[21]2122232425262728293031323334353637383940

[41]4142434445464748495051525354555657585960

[61]6162636465666768697071727374757677787980

[81]81828384858687888990919293949596979899100

[101]101102103104105106107108109110111112113114115116117118119120

[121]121122123124125126127128129130131132133134135136137138139140

[141]141142143144145146147148149150$class--表示类别

[1]"data.frame"查看数据集的前五项数据情况

>iris[1:

5,]

Sepal.LengthSepal.WidthPetal.LengthPetal.WidthSpecies

15.13.51.40.2setosa

24.93.01.40.2setosa

34.73.21.30.2setosa

44.63.11.50.2setosa

55.03.61.40.2setosa查看数据集中属性Sepal.Length前10行数据

>iris[1:

10,"Sepal.Length"]

[1]5.14.94.74.65.05.44.65.04.44.9同上

>iris$Sepal.Length[1:

10]

[1]5.14.94.74.65.05.44.65.04.44.9显示数据集中每个变量的分布情况

>summary(iris)

Sepal.LengthSepal.WidthPetal.LengthPetal.WidthSpecies

Min.:

4.300Min.:

2.000Min.:

1.000Min.:

0.100setosa:

50

1stQu.:

5.1001stQu.:

2.8001stQu.:

1.6001stQu.:

0.300versicolor:

50

Median:

5.800Median:

3.000Median:

4.350Median:

1.300virginica:

50

Mean:

5.843Mean:

3.057Mean:

3.758Mean:

1.199

3rdQu.:

6.4003rdQu.:

3.3003rdQu.:

5.1003rdQu.:

1.800

Max.:

7.900Max.:

4.400Max.:

6.900Max.:

2.500显示iris数据集列Species中各个值出现频次

>table(iris$Species)setosaversicolorvirginica

505050根据列Species画出饼图

>pie(table(iris$Species))算出列Sepal.Length的所有值的方差

>var(iris$Sepal.Length)

[1]0.6856935算出列iris$Sepal.Length和iris$Petal.Length的协方差

>cov(iris$Sepal.Length,iris$Petal.Length)

[1]1.274315算出列iris$Sepal.Length和iris$Petal.Length的相关系数,从结果看这两个值是强相关。

>cor(iris$Sepal.Length,iris$Petal.Length)

[1]0.8717538画出列iris$Sepal.Length分布柱状图

>hist(iris$Sepal.Length)画出列iris$Sepal.Length的密度函数图

>plot(density(iris$Sepal.Length))画出列iris$Sepal.Length和iris$Sepal.Width的散点图

>plot(iris$Sepal.Length,iris$Sepal.Width)绘出矩阵各列的散布图

>plot(iris)

or

>pairs(iris)第二步:

使用knn包进行Kmean聚类分析将数据集进行备份,将列newiris$Species置为空,将此数据集作为测试数据集

>newiris<-iris

>newiris$Species<-NULL在数据集newiris上运行Kmean聚类分析,将聚类结果保存在kc中。

在kmean函数中,将需要生成聚类数设置为3

>(kc<-kmeans(newiris,3))

K-meansclusteringwith3clustersofsizes38,50,62:

K-means算法产生了3个聚类,大小分别为38,50,62.Clustermeans:

每个聚类中各个列值生成的最终平均值

Sepal.LengthSepal.WidthPetal.LengthPetal.Width

15.0060003.4280001.4620000.246000

25.9016132.7483874.3935481.433871

36.8500003.0736845.7421052.071053Clusteringvector:

每行记录所属的聚类(2代表属于第二个聚类,1代表属于第一个聚类,3代表属于第三个聚类)

[1]111111111111111111111111111111111111

[37]111111111111112232222222222222222222

[73]222223222222222222222222222232333323

[109]333332233332323233223333323333233323

[145]332332Withinclustersumofsquaresbycluster:

每个聚类内部的距离平方和

[1]15.1510039.8209723.87947

(between_SS/total_SS=88.4%)组间的距离平方和占了整体距离平方和的的88.4%,也就是说各个聚类间的距离做到了最大Availablecomponents:

运行kmeans函数返回的对象所包含的各个组成部分

[1]"cluster""centers""totss""withinss"

[5]"tot.withinss""betweenss""size"

("cluster"是一个整数向量,用于表示记录所属的聚类

"centers"是一个矩阵,表示每聚类中各个变量的中心点

"totss"表示所生成聚类的总体距离平方和

"withinss"表示各个聚类组内的距离平方和

"tot.withinss"表示聚类组内的距离平方和总量

"betweenss"表示聚类组间的聚类平方和总量

"size"表示每个聚类组中成员的数量)创建一个连续表,在三个聚类中分别统计各种花出现的次数

>table(iris$Species,kc$cluster)

123

setosa0500

versicolor2048

virginica36014根据最后的聚类结果画出散点图,数据为结果集中的列"Sepal.Length"和"Sepal.Width",颜色为用1,2,3表示的缺省颜色

>plot(newiris[c("Sepal.Length","Sepal.Width")],col=kc$cluster)

在图上标出每个聚类的中心点

〉points(kc$centers[,c("Sepal.Length","Sepal.Width")],col=1:

3,pch=8,cex=2)

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 党团工作 > 入党转正申请

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1