【问题标题】:Clusterize similar words / values in R在 R 中聚类相似的词/值
【发布时间】:2018-07-15 13:16:22
【问题描述】:

假设我有以下变量

ChicKen120
Chicken1.20
Chicken(1.20)
Cow
cow.
cow/
cat

如您所见,有很多错别字。 我想做的是将相似的单词分组并自动重新定义每个组。

group 1 = "Cow", "cow", "cow/2

group 2 = "ChicKen120", "Chicken1.20"

格式化每个组后,最终结果将是

chicken(1.20)
chicken(1.20)
chicken(1.20)
cow
cow
cow
cat

我目前的进度

我通过adist()找到了相似词组

#Run adist on to find all words that are similar to another words. 
text <- c("ChicKen120","Chicken1.20","Chicken(1.20)","Cow","cow.", "cow/", "cat")
    > adist(text)
            [,1] [,2] [,3] [,4] [,5] [,6] [,7]
    [1,]    0    2    4    9    9    9    9
    [2,]    2    0    2   10    9   10   10
    [3,]    4    2    0   12   11   12   12
    [4,]    9   10   12    0    2    2    3
    [5,]    9    9   11    2    0    1    3
    [6,]    9   10   12    2    1    0    3
    [7,]    9   10   12    3    3    3    0

如你所见,相似词的距离小于4,不相似的词的距离大于4。

如何将这些结果聚类到可以重新定义的组中?

例如,我得到了以下建议:
“我使用 lapply() 和 unique() 对这个特征进行聚类。之后,我只是寻找质心并使用 table( ) 用于评分,就像检索互联网信息系统一样。例如:

“chocolate”、“chcolate”、“chocolatebar”、“choc bar”、“chocolate bar”都会自动重新识别为“chocolate”。

所有这些都是用原生库完成的。”

但是,我是 R 的初学者和数学的外行,所以我不知道如何处理组的聚类和重新定义。

【问题讨论】:

    标签: r grouping cluster-analysis hierarchical-clustering


    【解决方案1】:

    根据您的方法,您可以像这样继续聚类

    text <- c("ChicKen120","Chicken1.20","Chicken(1.20)","Cow","cow.", "cow/", "cat")
    mat <- adist(text)
    rownames(mat) <- colnames(mat) <- text
    
    d <- as.dist(mat)
    hc <- hclust(d, method = "average")
    plot(hc)
    
    k <- 2 # choose a 2-cluster-solution
    rect.hclust(hc, k=k)
    clusters <- cutree(hc, k=k)
    split(text, clusters)
    # $`1`
    # [1] "ChicKen120"    "Chicken1.20"   "Chicken(1.20)"
    # 
    # $`2`
    # [1] "Cow"  "cow." "cow/" "cat" 
    

    【讨论】:

    • 能否评论/解释发生了什么以及为什么会发生,以便我了解具体细节?
    • as.dist 将您的矩阵转换为距离矩阵。这可以由执行分层聚类分析的 hclust 处理。 Plot 绘制一个树状图,rect 在 2 个聚类周围绘制矩形,cutree 获取每个观察的聚类并 split 按聚类拆分观察。
    • 如果我有一个10000字的变量而不是上面7的例子,我该如何选择集群的大小?是否有自动确定集群大小的方法?
    • 在我的大字符向量上运行时,我收到以下消息mat &lt;- adist(text) Error: cannot allocate vector of size 3181.9 Gb In addition: Warning messages: 1: In adist(text) : Reached total allocation of 8052Mb: see help(memory.size) 是否有解决方法来实现与大向量相同的目标?
    • 你找到了吗?或者你能想到一个吗?
    【解决方案2】:

    问题在于区分大小写的输出。如果单词中的字母大小写不同,则生成的簇是错误的。所以我使用了以下方法来解决问题:sapply(list, tolower)

    【讨论】:

      猜你喜欢
      • 2019-06-07
      • 2013-03-22
      • 1970-01-01
      • 2020-11-28
      • 1970-01-01
      • 2015-04-05
      • 2018-01-07
      • 2011-08-16
      • 2016-04-21
      相关资源
      最近更新 更多