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



第一步:对数据集进行初步统计分析
检查数据的维度
> dim(iris)
[1] 150   5

显示数据集中的列名
> names(iris)
[1] "Sepal.Length" "Sepal.Width"  "Petal.Length" "Petal.Width"  "Species"     

显示数据集的内部结构
> str(iris)
'data.frame':   150 obs. of  5 variables:
$ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
$ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
$ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
$ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
$ Species     : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...

显示数据集的属性
> attributes(iris)
$names --就是数据集的列名
[1] "Sepal.Length" "Sepal.Width"  "Petal.Length" "Petal.Width"  "Species"     

$row.names --个人理解就是每行数据的标号
  [1]                    10  11  12  13  14  15  16  17  18  19  20
[21]  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40
[41]  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60
[61]  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80
[81]  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96  97  98  99 100
[101] 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
[121] 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
[141] 141 142 143 144 145 146 147 148 149 150

$class --表示类别
[1] "data.frame"

查看数据集的前五项数据情况
> iris[1:5,]
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
         5.1         3.5          1.4         0.2  setosa
         4.9         3.0          1.4         0.2  setosa
         4.7         3.2          1.3         0.2  setosa
         4.6         3.1          1.5         0.2  setosa
         5.0         3.6          1.4         0.2  setosa


查看数据集中属性Sepal.Length前10行数据
> iris[1:10, "Sepal.Length"]
[1] 5.1 4.9 4.7 4.6 5.0 5.4 4.6 5.0 4.4 4.9

同上
> iris$Sepal.Length[1:10]
[1] 5.1 4.9 4.7 4.6 5.0 5.4 4.6 5.0 4.4 4.9

显示数据集中每个变量的分布情况
> summary(iris)
  Sepal.Length    Sepal.Width     Petal.Length    Petal.Width          Species  
Min.   :4.300   Min.   :2.000   Min.   :1.000   Min.   :0.100   setosa    :50  
1st Qu.:5.100   1st Qu.:2.800   1st Qu.:1.600   1st Qu.:0.300   versicolor:50  
Median :5.800   Median :3.000   Median :4.350   Median :1.300   virginica :50  
Mean   :5.843   Mean   :3.057   Mean   :3.758   Mean   :1.199                  
3rd Qu.:6.400   3rd Qu.:3.300   3rd Qu.:5.100   3rd Qu.:1.800                  
Max.   :7.900   Max.   :4.400   Max.   :6.900   Max.   :2.500                  

显示iris数据集列Species中各个值出现频次
> table(iris$Species)

    setosa versicolor  virginica 
        50         50         50 

根据列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-means clustering with 3 clusters of sizes 38, 50, 62: K-means算法产生了3个聚类,大小分别为38,50,62. 

Cluster means: 每个聚类中各个列值生成的最终平均值
  Sepal.Length Sepal.Width Petal.Length Petal.Width
    5.006000    3.428000     1.462000    0.246000
    5.901613    2.748387     4.393548    1.433871
    6.850000    3.073684     5.742105    2.071053

Clustering vector: 每行记录所属的聚类(2代表属于第二个聚类,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 1 1 1 1 1 1 1 1 1 1 1 1 1
[37] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 3 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
[73] 2 2 2 2 2 3 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 2 3 3 3 3 2 3
[109] 3 3 3 3 3 2 2 3 3 3 3 2 3 2 3 2 3 3 2 2 3 3 3 3 3 2 3 3 3 3 2 3 3 3 2 3
[145] 3 3 2 3 3 2

Within cluster sum of squares by cluster: 每个聚类内部的距离平方和   
[1] 15.15100 39.82097 23.87947
(between_SS / total_SS =  88.4 %) 组间的距离平方和占了整体距离平方和的的88.4%,也就是说各个聚类间的距离做到了最大


Available components: 运行kmeans函数返回的对象所包含的各个组成部分
[1] "cluster"      "centers"      "totss"        "withinss"    
[5] "tot.withinss" "betweenss"    "size"  
("cluster"是一个整数向量,用于表示记录所属的聚类  
"centers"是一个矩阵,表示每聚类中各个变量的中心点
"totss"表示所生成聚类的总体距离平方和
"withinss"表示各个聚类组内的距离平方和
"tot.withinss"表示聚类组内的距离平方和总量
"betweenss"表示聚类组间的聚类平方和总量
"size"表示每个聚类组中成员的数量)

创建一个连续表,在三个聚类中分别统计各种花出现的次数
> table(iris$Species, kc$cluster)           
                3
  setosa      0 50  0
  versicolor   0 48
  virginica  36  0 14

根据最后的聚类结果画出散点图,数据为结果集中的列"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)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值