K-模式集群验证

问题描述 投票:0回答:1

到目前为止,我发现使用 k 模式进行内部集群验证的选项很少。

但是,我最近发现一篇论文使用了称为 SW/SB 比率的验证指标。

  • SW = 组的标准差。
  • SB = 组间标准差。

然而,我个人还没有在 R 中看到使用标准 k-modes 包(例如 klaR)的示例。

因此,我想知道是否有人能够使用玩具数据集展示 R 中 SW/SB 比率的有效示例?

所有帮助将不胜感激。

library(klaR)

x1 = rep(1:3, times = 40)
x2 = rep(1:3, times = 40)
x3 = rep(1:3, times = 40)
x4 = rep(1:3, times = 40)
x5 = rep(1:3, times = 40)

dat <- data.frame(x1, x2, x3, x4, x5)

km <- kmodes(dat, 3)

原论文在这里。 https://iopscience.iop.org/article/10.1088/1757-899X/1087/1/012085/pdf

r machine-learning cluster-analysis kmodes
1个回答
0
投票

doc 可以看出,函数

kmodes()
需要分类数据。 让我们尝试从以下数据样本中了解 SW(簇内方差)在这种情况下可能意味着什么。

由于简单的欧几里得距离或曼哈顿距离不适用于 calcategories 变量(至少当它们是 one-hot 编码时),因此

klaR
实现使用简单的匹配距离来查找 SW。

让我们从以下数据集开始:

set.seed(1)
x <- rbind(matrix(rbinom(100, 2, 0.25), ncol = 2),
           matrix(rbinom(100, 2, 0.75), ncol = 2))
colnames(x) <- c("a", "b")
head(x)
#      a b
# [1,] 0 0
# [2,] 0 1
# [3,] 1 0
# [4,] 1 0
# [5,] 0 0
# [6,] 1 0

plot(jitter(x), col = cl$cluster, pch=19, main=paste(k, 'clusters'))

从上面可以看出,这个玩具数据集显然包含 9 个簇。因此,让我们尝试使用

kmodes
算法创建 9 个簇:

k <- 9
(cl <- kmodes(x, k))
# K-modes clustering with 9 clusters of sizes 18, 12, 8, 1, 11, 14, 18, 15, 3

# Cluster modes:
#  a b
#1 0 0
#2 1 0
#3 0 1
#4 2 0
#5 1 2
#6 2 2
#7 1 1
#8 2 1
#9 0 2

# Clustering vector:
# 1 3 2 2 1 2 4 2 7 1 3 1 2 1 7 1 2 8 1 7 2 3 2 1 1 3 3 1 7 9 1 7 1 1 7 2 7 1 
# 2 1 2 2 7 3 3 7 1 1 7 7 7 6 6 9 5 6 6 6 5 5 9 7 6 8 8 6 5 6 8 5 3 8 8 6 5
# 8 8 8 8 7 5 6 8 5 7 5 8 8 1 6 7 5 6 8 5 8 6 7 6 7

# Within cluster simple-matching distance by cluster:
# [1] 0 0 0 0 0 0 0 0 0

plot(jitter(x), col = cl$cluster, pch=19, main=paste(k, 'clusters'))

从上面可以看出,这会产生完美的聚类,因为所有聚类的聚类内简单匹配距离都是 0。

现在,让我们尝试一下

k=5

k <- 9
(cl <- kmodes(x, k))
# K-modes clustering with 5 clusters of sizes 24, 17, 27, 17, 15

# Cluster modes:
#  a b
#1 0 0
#2 1 0
#3 1 1
#4 2 1
#5 2 2

# Clustering vector:
# 1 3 2 2 1 2 4 2 3 1 1 1 2 1 3 1 2 4 1 3 2 4 2 1 1 3 1 1 3 1 1 3 1 1 3 2 3 1 2 
# 1 2 2 3 3 1 3 1 1 3 3 3 5 5 1 3 5 5 5 2 3 5 3 5 4 4 5 3 5 4 3 1 4 4 5 2
# 4 4 4 4 3 3 5 4 2 3 2 4 4 1 5 3 3 5 4 2 4 5 3 5 3

# Within cluster simple-matching distance by cluster:
# 6 5 9 2 1

plot(jitter(x), col = cl$cluster, pch=19, main=paste(k, 'clusters'))

注意这次的簇内简单匹配距离。现在让我们找到分组到簇 3 中的点,例如,簇 3 的相应模式为 (1,1)。

x[cl$cluster == 3,]
      a b
 [1,] 0 1   # mismatch in 1 position with (1,1)
 [2,] 1 1
 [3,] 1 1
 [4,] 1 1
 [5,] 0 1   # mismatch in 1 position with (1,1)
 [6,] 1 1
 [7,] 1 1
 [8,] 1 1
 [9,] 1 1
[10,] 1 1
[11,] 0 1   # mismatch in 1 position with (1,1)
[12,] 1 1
[13,] 1 1
[14,] 1 1
[15,] 1 1
[16,] 1 2   # mismatch in 1 position with (1,1)
[17,] 1 2   # mismatch in 1 position with (1,1)
[18,] 1 1   
[19,] 1 2   # mismatch in 1 position with (1,1)
[20,] 1 2   # mismatch in 1 position with (1,1)
[21,] 1 1
[22,] 1 2   # mismatch in 1 position with (1,1)
[23,] 1 1
[24,] 1 1
[25,] 1 2   # mismatch in 1 position with (1,1)
[26,] 1 1
[27,] 1 1

因此,集群 3 的 9 个位置存在不匹配,这也在上面的集群内简单匹配距离中报告,这可以认为是 SW。

SW 之和。即,所有此类簇内距离的 SSW 可以通过对所有簇中的这些数字求和来计算。随着簇数量的增加,SSW 应该会减少。这个 SSW 本身可以用作集群验证指标。

例如,让我们尝试绘制 SSW 如何随簇数变化

k

num_clusters <- 2:9
SSW <- c()
for (k in num_clusters) {
  cl <- kmodes(x, k)
  SSW <- c(SSW, sum(cl$withindiff))
}
plot(num_clusters, SSW, pch=19, col='red', main='SW vs. #clusters')
lines(num_clusters, SSW)

我们可以以类似的方式计算类间方差 SSB 并计算比率。

另一种方法可能是对分类值使用单热编码 (OHE),并使用欧几里得距离来计算 SSW 和 SSB,使用此处中的公式并计算 SSW/SSB 进行验证。

希望这有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.