5.2、聚类之层次聚类实例

实例一、对莺尾花数据进行层次聚类分类

#1、加载数据
data(iris)
newiris <- iris[, 1:4]
iris_scale <- scale(newiris)
#注意:层次聚类使用的是欧式聚类,所以需要对数据进行标准化和中心化处理

#2、建模
model_hclust <- hclust(dist(iris_scale))

#3、模型评估
plot(model_hclust, hang=-1)
#hang是表明谱系图中各类所在的位置 当hang取负值时,谱系图中的类从底部画起。若是负数,则表示末端树杈长度是0,即标签对齐。
(re <- rect.hclust(model_hclust, k=3))
## [[1]]
##  [1]  51  52  53  55  57  59  62  64  65  66  67  71  72  73  74  75  76
## [18]  77  78  79  84  85  86  87  89  92  96  97  98 101 102 103 104 105
## [35] 106 108 109 110 111 112 113 114 115 116 117 118 119 121 122 123 124
## [52] 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
## [69] 142 143 144 145 146 147 148 149 150
## 
## [[2]]
##  [1]  42  54  56  58  60  61  63  68  69  70  80  81  82  83  88  90  91
## [18]  93  94  95  99 100 107 120
## 
## [[3]]
##  [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
## [24] 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 43 44 45 46 47
## [47] 48 49 50
(model_id <- cutree(model_hclust, 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
##  [36] 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 3 3 3 2 3 2 3 2 3 2 2 3 2 3 3 3 3 2 2 2
##  [71] 3 3 3 3 3 3 3 3 3 2 2 2 2 3 3 3 3 2 3 2 2 3 2 2 2 3 3 3 2 2 3 3 3 3 3
## [106] 3 2 3 3 3 3 3 3 3 3 3 3 3 3 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
## [141] 3 3 3 3 3 3 3 3 3 3
table(iris[, 5],model_id)
##             model_id
##               1  2  3
##   setosa     49  1  0
##   versicolor  0 21 29
##   virginica   0  2 48
#4、生成聚类图
library(fpc)

plotcluster((iris[, 1:4]), model_id)

实例二、各省消费数据

#1、加载数据
X<-data.frame(
  x1=c(2959.19, 2459.77, 1495.63, 1046.33, 1303.97, 1730.84,
       1561.86, 1410.11, 3712.31, 2207.58, 2629.16, 1844.78,
       2709.46, 1563.78, 1675.75, 1427.65, 1783.43, 1942.23,
       3055.17, 2033.87, 2057.86, 2303.29, 1974.28, 1673.82,
       2194.25, 2646.61, 1472.95, 1525.57, 1654.69, 1375.46,
       1608.82),
  x2=c(730.79, 495.47, 515.90, 477.77, 524.29, 553.90, 492.42,
       510.71, 550.74, 449.37, 557.32, 430.29, 428.11, 303.65,
       613.32, 431.79, 511.88, 512.27, 353.23, 300.82, 186.44,
       589.99, 507.76, 437.75, 537.01, 839.70, 390.89, 472.98,
       437.77, 480.99, 536.05),
  x3=c(749.41, 697.33, 362.37, 290.15, 254.83, 246.91, 200.49,
       211.88, 893.37, 572.40, 689.73, 271.28, 334.12, 233.81,
       550.71, 288.55, 282.84, 401.39, 564.56, 338.65, 202.72,
       516.21, 344.79, 461.61, 369.07, 204.44, 447.95, 328.90,
       258.78, 273.84, 432.46),
  x4=c(513.34, 302.87, 285.32, 208.57, 192.17, 279.81, 218.36,
       277.11, 346.93, 211.92, 435.69, 126.33, 160.77, 107.90,
       219.79, 208.14, 201.01, 206.06, 356.27, 157.78, 171.79,
       236.55, 203.21, 153.32, 249.54, 209.11, 259.51, 219.86,
       303.00, 317.32, 235.82),
  x5=c(467.87, 284.19, 272.95, 201.50, 249.81, 239.18, 220.69,
       224.65, 527.00, 302.09, 514.66, 250.56, 405.14, 209.70,
       272.59, 217.00, 237.60, 321.29, 811.88, 329.06, 329.65,
       403.92, 240.24, 254.66, 290.84, 379.30, 230.61, 206.65,
       244.93, 251.08, 250.28),
  x6=c(1141.82, 735.97, 540.58, 414.72, 463.09, 445.20, 459.62,
       376.82, 1034.98, 585.23, 795.87, 513.18, 461.67, 393.99,
       599.43, 337.76, 617.74, 697.22, 873.06, 621.74, 477.17,
       730.05, 575.10, 445.59, 561.91, 371.04, 490.90, 449.69,
       479.53, 424.75, 541.30),
  x7=c(478.42, 570.84, 364.91, 281.84, 287.87, 330.24, 360.48,
       317.61, 720.33, 429.77, 575.76, 314.00, 535.13, 509.39,
       371.62, 421.31, 523.52, 492.60, 1082.82, 587.02, 312.93,
       438.41, 430.36, 346.11, 407.70, 269.59, 469.10, 249.66,
       288.56, 228.73, 344.85),
  x8=c(457.64, 305.08, 188.63, 212.10, 192.96, 163.86, 147.76,
       152.85, 462.03, 252.54, 323.36, 151.39, 232.29, 160.12,
       211.84, 165.32, 182.52, 226.45, 420.81, 218.27, 279.19,
       225.80, 223.46, 191.48, 330.95, 389.33, 191.34, 228.19,
       236.51, 195.93, 214.40),
  row.names=c("北京","天津","河北","山西","内蒙古",
              "辽宁","吉林","黑龙江","上海","江苏","浙江",
              "安徽","福建","江西","山东","河南","湖北",
              "湖南","广东","广西","海南","重庆","四川",
              "贵州","云南","西藏","陕西","甘肃","青海" ,
              "宁夏","新疆")          
)

#2、数据预处理:标准化和中心化
dist <- dist(scale(X))

#3、聚类
model_hclust <- hclust(dist)

#4、绘制聚类图
plot(model_hclust, hang=-1)
rect_id <- rect.hclust(model_hclust, k=5, border = "blue")

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值