Mar*_*ius 13 r k-means least-squares
我在R中使用kmeans()函数,我很好奇返回对象的totss和tot.withinss属性之间的区别是什么.从文档中他们似乎返回相同的东西,但在我的数据集上应用totss的值是66213.63,而tot.withinss的值是6893.50.如果您熟悉mroe细节,请告诉我.谢谢!
马吕斯.
G. *_*eck 19
给定betweenss每个聚类之间的平方和与平方和之间的向量之间withinss的公式如下:
totss = tot.withinss + betweenss
tot.withinss = sum(withinss)
例如,如果只有一个簇,然后betweenss将是0,将有只有一个部件withinss和totss = tot.withinss = withinss.
为了进一步说明,我们可以根据群集分配自己计算这些不同的数量,这可能有助于澄清它们的含义.考虑一下示例中的数据x和集群分配.定义平方和函数如下 - 这将从该列中减去每列x的平均值,然后减去剩余矩阵的每个元素的平方和:cl$clusterhelp(kmeans)
# or ss <- function(x) sum(apply(x, 2, function(x) x - mean(x))^2)
ss <- function(x) sum(scale(x, scale = FALSE)^2)
然后我们有以下内容.注意,它cl$centers[cl$cluster, ]是拟合值,即它是一个矩阵,每个点有一行,这样第i行就是第i个点所属的簇的中心.
example(kmeans) # create x and cl
betweenss <- ss(cl$centers[cl$cluster,]) # or ss(fitted(cl))
withinss <- sapply(split(as.data.frame(x), cl$cluster), ss)
tot.withinss <- sum(withinss) # or  resid <- x - fitted(cl); ss(resid)
totss <- ss(x) # or tot.withinss + betweenss
cat("totss:", totss, "tot.withinss:", tot.withinss, 
  "betweenss:", betweenss, "\n")
# compare above to:
str(cl)
编辑:
由于回答了这个问题,R已经添加了其他类似的kmeans例子(example(kmeans))和一个新fitted.kmeans方法,现在我们在尾随代码行的注释中展示了拟合方法如何适合上面的内容.