【发布时间】:2017-06-11 22:58:09
【问题描述】:
我必须关注数据:
attributes <- c("apple-water-orange", "apple-water", "apple-orange", "coffee", "coffee-croissant", "green-red-yellow", "green-red-blue", "green-red","black-white","black-white-purple")
attributes
attributes
1 apple-water-orange
2 apple-water
3 apple-orange
4 coffee
5 coffee-croissant
6 green-red-yellow
7 green-red-blue
8 green-red
9 black-white
10 black-white-purple
我想要的是另一列,它根据观察相似性为每一行分配一个类别。
category <- c(1,1,1,2,2,3,3,3,4,4)
df <- as.data.frame(cbind(df, category))
attributes category
1 apple-water-orange 1
2 apple-water 1
3 apple-orange 1
4 coffee 2
5 coffee-croissant 2
6 green-red-yellow 3
7 green-red-blue 3
8 green-red 3
9 black-white 4
10 black-white-purple 4
这是广义上的聚类,但我认为大多数聚类方法仅适用于数字数据,并且单热编码有很多缺点(这是我在互联网上看到的)。
有人知道如何完成这项任务吗?也许一些单词匹配方法?
如果我可以根据参数调整相似度(粗略与体面的“聚类”),那就太好了。
提前感谢您的任何想法!
【问题讨论】:
-
This post 可能有帮助。
-
聚类适用于距离,无论数据类型如何。您可以使用多种不同的方法计算字符串之间的距离。
-
看来第一个字总是一样的?那么也许
match(sub('-.*', '', attributes), unique(sub('-.*', '', attributes)))就足够了吗? -
这在很大程度上取决于实际数据的外观。 (本地)编辑距离可能是一个不错的选择(您上面显示的反例在本地编辑距离方面得分很高),并且可以扩展该算法以允许反转。如果您只想基于单词匹配进行聚类,将字符串拆分为单词并量化单词的重叠(
length(intersect(words_vector_1, words_vector2)))会更简单、更高效。
标签: r cluster-analysis string-matching categorical-data textmatching