计算 R 中簇之间的总平方和

Calculate total sum of squares between clusters in R

我的objective是比较我用过的两种聚类方法cluster_method_1cluster_method_2中哪一种的聚类平方和最大,以确定哪一种达到更好的分离。

我基本上是在寻找一种有效的方法来计算聚类 1 的每个点与聚类 2、3、4 等的所有点之间的距离。

示例数据框:

structure(list(x1 = c(0.01762376, -1.147739752, 1.073605848, 
2.000420899, 0.01762376, 0.944438811, 2.000420899, 0.01762376, 
-1.147739752, -1.147739752), x2 = c(0.536193126, 0.885609849, 
-0.944699546, -2.242627057, -1.809984553, 1.834120637, 0.885609849, 
0.96883563, 0.186776403, -0.678508604), x3 = c(0.64707104, -0.603759684, 
-0.603759684, -0.603759684, -0.603759684, 0.64707104, -0.603759684, 
-0.603759684, -0.603759684, 1.617857394), x4 = c(-0.72712328, 
0.72730861, 0.72730861, -0.72712328, -0.72712328, 0.72730861, 
0.72730861, -0.72712328, -0.72712328, -0.72712328), cluster_method_1 = structure(c(1L, 
3L, 3L, 3L, 2L, 2L, 3L, 2L, 1L, 4L), .Label = c("1", "2", "4", 
"6"), class = "factor"), cluster_method_2 = structure(c(5L, 3L, 
1L, 3L, 4L, 2L, 1L, 1L, 1L, 6L), .Label = c("1", "2", "3", "4", 
"5", "6"), class = "factor")), row.names = c(NA, -10L), class = c("tbl_df", 
"tbl", "data.frame"))



        x1     x2     x3     x4 cluster_method_1 cluster_method_2
     <dbl>  <dbl>  <dbl>  <dbl> <fct>            <fct>           
 1  0.0176  0.536  0.647 -0.727 1                5               
 2 -1.15    0.886 -0.604  0.727 4                3               
 3  1.07   -0.945 -0.604  0.727 4                1               
 4  2.00   -2.24  -0.604 -0.727 4                3               
 5  0.0176 -1.81  -0.604 -0.727 2                4               
 6  0.944   1.83   0.647  0.727 2                2               
 7  2.00    0.886 -0.604  0.727 4                1               
 8  0.0176  0.969 -0.604 -0.727 2                1               
 9 -1.15    0.187 -0.604 -0.727 1                1               
10 -1.15   -0.679  1.62  -0.727 6                6  

聚类 Si 的内部平方和可以写成所有成对(欧几里得)距离的平方和,除以该簇中点数的两倍(参见 the Wikipedia article on k-means clustering

为方便起见,我们定义了一个函数 calc_SS,其中 return 是(数字)data.frame

的内部平方和
calc_SS <- function(df) sum(as.matrix(dist(df)^2)) / (2 * nrow(df))

然后可以直接计算每个方法的每个簇的(簇内)平方和

library(tidyverse)
df %>%
    gather(method, cluster, cluster_method_1, cluster_method_2) %>%
    group_by(method, cluster) %>%
    nest() %>%
    transmute(
        method,
        cluster,
        within_SS = map_dbl(data, ~calc_SS(.x))) %>%
    spread(method, within_SS)
## A tibble: 6 x 3
#  cluster cluster_method_1 cluster_method_2
#  <chr>              <dbl>            <dbl>
#1 1                   1.52             9.99
#2 2                  10.3              0
#3 3                  NA               10.9
#4 4                  15.2              0
#5 5                  NA                0
#6 6                   0                0

平方和内的总数就是每个簇

内平方和的总和
df %>%
    gather(method, cluster, cluster_method_1, cluster_method_2) %>%
    group_by(method, cluster) %>%
    nest() %>%
    transmute(
        method,
        cluster,
        within_SS = map_dbl(data, ~calc_SS(.x))) %>%
    group_by(method) %>%
    summarise(total_within_SS = sum(within_SS)) %>%
    spread(method, total_within_SS)
## A tibble: 1 x 2
#  cluster_method_1 cluster_method_2
#             <dbl>            <dbl>
#1             27.0             20.9 

顺便说一句,我们可以使用 iris 数据集确认 calc_SS 确实 return 内平方和:

set.seed(2018)
df2 <- iris[, 1:4]
kmeans <- kmeans(as.matrix(df2), 3)
df2$cluster <- kmeans$cluster

df2 %>%
    group_by(cluster) %>%
    nest() %>%
    mutate(within_SS = map_dbl(data, ~calc_SS(.x))) %>%
    arrange(cluster)
## A tibble: 3 x 3
#  cluster data              within_SS
#    <int> <list>                <dbl>
#1       1 <tibble [38 × 4]>      23.9
#2       2 <tibble [62 × 4]>      39.8
#3       3 <tibble [50 × 4]>      15.2

kmeans$within
#[1] 23.87947 39.82097 15.15100

总平方和,sum_x sum_y ||x-y||² 是常量。

可以根据方差简单地计算总平方和。

如果现在减去簇内平方和,其中x和y属于同一个簇,则簇间平方和仍然存在。

如果采用这种方法,则需要 O(n) 时间而不是 O(n²)。

推论:具有最小 WCSS 的解决方案具有最大的 BCSS。

考虑包 clValid。它计算大量索引以验证聚类。邓恩指数特别适合您正在尝试做的事情。文档说,邓恩指数是不在同一集群中的观察之间的最小距离与最大 intra-cluster 距离之间的比率。它的文档可以在 https://cran.r-project.org/web/packages/clValid/clValid.pdf.

找到