R 中 K 均值聚类的问题

Problems with K-means clustering in R

当我尝试对标准虹膜数据进行 K 均值聚类时

library('tidyverse')
iris_Cluster <- kmeans(iris[, 3:4], 2, nstart = 10)
iris$cluster <- as.factor(iris_Cluster$cluster)
p_iris <- ggplot(iris, aes(x = Petal.Length, y = Petal.Width, color=cluster)) + geom_point()
print(p_iris)

我得到一分属于错误的簇。问题是什么?这是K-means聚类算法的弱点吗?如何得到合适的结果?什么是分区聚类的好算法?

是的,根据平方和objective,这个点属于红色簇。

考虑一下,例如 DBSCAN。

属于"wrong"簇的点是点99。 它有 Petal.Length = 3 和 Petal.Width = 1.1。你可以获得 来自

的集群中心
iris_Cluster$centers
  Petal.Length Petal.Width
1     4.925253   1.6818182
2     1.492157   0.2627451

您可以使用

查看从点 99 到聚类中心的距离
as.matrix(dist(rbind(iris_Cluster$centers, iris[99,3:4])))
          1        2       99
1  0.000000 3.714824 2.011246
2  3.714824 0.000000 1.724699
99 2.011246 1.724699 0.000000

点 99 更接近聚类中心 (1.49, 0.26)。 问题是 k-means 选择的聚类中心是 最接近一个点,而不是基于事物有意义的中心 就像附近点的集群。正如@Anony-Mousse 所建议的那样, DBSCAN 可能更符合您的喜好。 DB 部分代表 Density Based 它创建了集群,其中的点可以通过 高密度区域。另一种选择是单link层级 聚类倾向于将彼此靠近的点放在 同一个集群。

模仿您的代码但使用 hclust:

library(ggplot2)
iris_HC <- hclust(dist(iris[,3:4]), method="single")
iris_Cluster <- cutree(iris_HC, 2)
iris$cluster <- as.factor(iris_Cluster)

p_iris <- ggplot(iris, aes(x=Petal.Length, y=Petal.Width, color=cluster)) + geom_point()
print(p_iris)