R K的最佳值应该是什么?K表示在任何数据集上实现聚类?

R K的最佳值应该是什么?K表示在任何数据集上实现聚类?,r,R,就像问题所说的那样,我正在制作一个可视化工具,该工具必须适用于提供的任何数据集。我应该选择什么样的最佳K值以及如何选择?因此,您可以使用素食套餐中的卡林斯基标准,而且您的问题措辞几乎没有争议。我希望这是你所期待的,请在其他情况下发表评论 例如,您可以执行以下操作: n = 100 g = 6 set.seed(g) d <- data.frame( x = unlist(lapply(1:g, function(i) rnorm(n/g, runif(1)*i^2))), y

就像问题所说的那样,我正在制作一个可视化工具,该工具必须适用于提供的任何数据集。我应该选择什么样的最佳K值以及如何选择?

因此,您可以使用素食套餐中的
卡林斯基标准,而且您的问题措辞几乎没有争议。我希望这是你所期待的,请在其他情况下发表评论

例如,您可以执行以下操作:

n = 100
g = 6 
set.seed(g)
d <- data.frame(
  x = unlist(lapply(1:g, function(i) rnorm(n/g, runif(1)*i^2))), 
  y = unlist(lapply(1:g, function(i) rnorm(n/g, runif(1)*i^2))))

require(vegan)
fit <- cascadeKM(scale(d, center = TRUE,  scale = TRUE), 1, 10, iter = 1000)
plot(fit, sortg = TRUE, grpmts.plot = TRUE)
calinski.best <- as.numeric(which.max(fit$results[2,]))
cat("Calinski criterion optimal number of clusters:", calinski.best, "\n")
n=100
g=6
结实种子(克)
d这会更好,虽然它也可能太宽了。聚类的首要目标是什么?不存在对任何数据集都是最优的“通用”最优k值。
wss <- (nrow(d)-1)*sum(apply(d,2,var))
#TSS = WSS ##No betweeness at first observation, total variance equal to withness variance, TSS is total sum of squares, WSS is within sum of squress
for (i in 2:15) wss[i] <- sum(kmeans(d,centers=i)$withinss) #from second observation onward, since TSS would remain constant and between sum of squares will increase, correspondingly withiness would decrease.
#Plotting the same using the plot command for 15 iterations.(This is not constant, you have to decide what iterations you can do here.
plot(1:15, wss, type="b", xlab="Number of Clusters",
     ylab="Within groups sum of squares",col="mediumseagreen",pch=12)