【问题标题】:identifying data points in a cluster识别集群中的数据点
【发布时间】:2016-09-24 00:10:35
【问题描述】:

我正在 R 中创建一个 k 表示集群。任何人都可以帮助我确定如果集群形成并且我想访问属于特定集群的数据点,我该怎么做?

【问题讨论】:

  • 请提供一些代码。这将使以有用的方式更容易回答。
  • 欢迎来到 Stack Overflow!请阅读有关how to ask a good question 的信息以及如何提供reproducible example。这将使其他人更容易帮助您。

标签: r cluster-analysis k-means


【解决方案1】:

这里有一个简单的例子:使用 k-means 基于 Petal.LengthPetal.Width 的值使用三个中心对 iris 集进行聚类:

k_cluster <- kmeans(iris[c("Petal.Length","Petal.Width")], 3)
#>k_cluster
#K-means clustering with 3 clusters of sizes 50, 54, 46
#
#Cluster means:
#  Petal.Length Petal.Width
#1     1.462000    0.246000
#2     4.292593    1.359259
#3     5.626087    2.047826

#Clustering vector:
#  [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2
# [56] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 2 2 2 2 2 3 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 2 3 3 3
#[111] 3 3 3 3 3 3 3 3 3 2 3 3 3 2 3 3 2 2 3 3 3 3 3 3 3 3 3 3 2 3 3 3 3 3 3 3 3 3 3 3

#Within cluster sum of squares by cluster:
#[1]  2.02200 14.22741 15.16348
# (between_SS / total_SS =  94.3 %)

将条目(iris 集合的行)分配给三个集群之一存储在集群向量 k_cluster$cluster 中。因此,要访问属于 3 号集群的条目,可以使用

iris[k_cluster$cluster==3,]
#> head(iris[k_cluster$cluster==3,])
#   Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
#51          7.0         3.2          4.7         1.4 versicolor
#52          6.4         3.2          4.5         1.5 versicolor
#54          5.5         2.3          4.0         1.3 versicolor
#55          6.5         2.8          4.6         1.5 versicolor
#56          5.7         2.8          4.5         1.3 versicolor
#57          6.3         3.3          4.7         1.6 versicolor

还有几种可视化集群的方法。但是,鉴于目前提出问题的一般形式,似乎不适合进一步详细说明。

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 2018-09-11
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-18
    • 2019-09-26
    • 2018-10-24
    相关资源
    最近更新 更多