【问题标题】:Classify a list of element based on their attribute in R根据元素在 R 中的属性对元素列表进行分类
【发布时间】:2015-12-02 21:07:43
【问题描述】:

我有一个列表元素,每个元素都包含一个文本属性列表,例如 .

> list
[[1]]
 [1] "attribute 1"     
 [2] "attribute 2"     
 [3] "attribute 3"     

[[2]]
[1] "attribute 4"     
[2] "attribute 5" 
[3] "attribute 6" 

[[3]]
 [1] "attribute 1"     
 [2] "attribute 2"      

[[4]]
[1] "attribute 4"     
[2] "attribute 5"
[3] "attribute 6" 

我可以应用什么分类或聚类算法(最简单的)来根据文本属性的相似性对该元素进行分类。
得到如下结果:类别 1 中的 [1,3] 和类别 2 中的 [2,4] 。

【问题讨论】:

  • 你想要的输出不是很清楚。您最好提供列表中的dput

标签: r classification


【解决方案1】:

想法

您可以在距离矩阵上使用hclust。为此,您首先需要将数据转换为矩阵,计算距离,然后对该矩阵进行层次聚类。

代码

l <- list(paste("attribute", 1:3),
          paste("attribute", 4:6),
          paste("attribute", 1:2),
          paste("attribute", 4:6))
allElem <- sort(unique(unlist(l)))
incidM <- do.call(rbind, lapply(l, function(x) as.numeric(allElem %in% x)))
colnames(incidM) <- allElem
rownames(incidM) <- paste("Set", seq_len(NROW(incidM)))
dM <- dist(incidM)
hc <- hclust(dM)
plot(hc)

说明

首先,您创建一个矩阵,其中行对应于列表中的元素,行对应于列表中的唯一值。如果对应的列表元素包含此属性,则每个元素要么为1,否则为0

incidM
#       attribute 1 attribute 2 attribute 3 attribute 4 attribute 5 attribute 6
# Set 1           1           1           1           0           0           0
# Set 2           0           0           0           1           1           1
# Set 3           1           1           0           0           0           0
# Set 4           0           0           0           1           1           1

然后,您可以计算行之间的距离矩阵并对该矩阵进行层次聚类。最后你可以画出整个事情,你确实看到 Set 1 & 3 和 Set 2 & 4 是相似的。

【讨论】:

    猜你喜欢
    • 2021-09-02
    • 2016-03-02
    • 1970-01-01
    • 2019-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-01
    相关资源
    最近更新 更多