【发布时间】:2020-05-06 21:44:22
【问题描述】:
有一个任务是在 R 中使用包含分类文本值(如“蓝色”、“红色”)的数据集进行聚类。 k-means 算法虽然只接受数值,所以我需要将这些文本类别转换为 int 类别。是否有任何 1 线解决方案?或者其他?
Attribute1 Attribute2 Attribute1 Attribute2
Instance1 "blue" "red" => Instance1 1 2
Instance2 "green" "blue" Instance2 3 1
a[,1] <- sapply(a[,1],switch,"blue"=1,"red"=2,"green"=3)
完成任务,但对于“a”数据框的第一列
x_num <- as.numeric(x)
与众不同,但看起来很像。
【问题讨论】:
-
你不能那样做,如果你把 "blue", "red" 转换成 1,2 你是在暗示某种我猜不是这样的顺序?
-
你应该做one-hot编码,或者尝试高尔距离进行聚类,例如r-bloggers.com/clustering-mixed-data-types-in-r
-
是的,非常感谢。这就是我一直在寻找的
-
很高兴它有帮助:)
标签: r dataframe cluster-analysis data-manipulation