k-means的分类数目

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/G090909/article/details/54629792

k-means聚类的类数确定

根据类内离差平方和最小,类间离差平方和最大的原则

自定义函数

tot.wssplot <- function(data, nc, seed=1234){
#假设分为一组时的总的离差平方和
tot.wss <- (nrow(data)-1)*sum(apply(data,2,var))
for (i in 2:nc){
#必须指定随机种子数
set.seed(seed)
tot.wss[i] <- kmeans(data, centers=i, iter.max = 100)$tot.withinss
}
plot(1:nc, tot.wss, type=”b”, xlab=”Number of Clusters”,
ylab=”Within groups sum of squares”,col = ‘blue’,
lwd = 2, main = ‘Choose best Clusters’)
}

kmeans()函数的返回值中,tot.withiness表示总的类内平方和;withiness向量表示每个类的组内平方和。

猜你喜欢

转载自blog.csdn.net/G090909/article/details/54629792