聚类分析-层次聚类法

聚类分析是一种数据规约技术,可以把大量观测值规约为若干类。

最常用的两种聚类方法是层次聚类法(hierarchical agglomerative clustering)和划分聚类法(partitioning clustering)。层次聚类是通过计算不同类别数据点间的相似度来创建一棵有层次的嵌套聚类树。今天我们来一起看一下层次聚类法的几种常见方法。

聚类分析-层次聚类法_第1张图片

目的:根据2018年1月气温状况对全国省会城市进行分类

##准备数据

聚类分析-层次聚类法_第2张图片

#设置路径

getwd()

setwd("D:/rdata")

temp.hc <- hclust(distance) #聚类分析,最长距离法

#读入数据

temp <- read.table("urbantemp.txt",header=TRUE)

distance <- dist(temp) #计算距离

plot(temp.hc, hang = -1) #绘画系谱图

re <- rect.hclust(temp.hc, k = 5) #分为5类

re

聚类分析-层次聚类法_第3张图片

temp.hc <- hclust(distance,method="single") #聚类分析,最短距离法

plot(temp.hc, hang = -1) #绘画系谱图

re <- rect.hclust(temp.hc, k = 5) #分为5类

聚类分析-层次聚类法_第4张图片

temp.hc <- hclust(distance,method="average") #聚类分析,类平均法

plot(temp.hc, hang = -1) #绘画系谱图

re <- rect.hclust(temp.hc, k = 5) #分为5类

聚类分析-层次聚类法_第5张图片

temp.hc <- hclust(distance,method="centroid") #聚类分析,重心法

plot(temp.hc, hang = -1) #绘画系谱图

re <- rect.hclust(temp.hc, k = 5) #分为5类

聚类分析-层次聚类法_第6张图片

temp.hc <- hclust(distance,method="median") #聚类分析,中间距离法

plot(temp.hc, hang = -1) #绘画系谱图

re <- rect.hclust(temp.hc, k = 5) #分为5类

聚类分析-层次聚类法_第7张图片

temp.hc <- hclust(distance,method="ward") #聚类分析,离差平方和法

plot(temp.hc, hang = -1) #绘画系谱图re <- rect.hclust(temp.hc, k = 5) #分为5类

聚类分析-层次聚类法_第8张图片

                                   更多精彩作品,长按二维码关注我们

聚类分析-层次聚类法_第9张图片

你可能感兴趣的:(聚类分析-层次聚类法)