【发布时间】:2019-05-02 23:38:38
【问题描述】:
我正在尝试在一个相对简单的数据框上运行 kmeans 聚类分析。但是,
kmeans(sample_data, centers = 4)
不起作用,因为 R 声明“外部函数调用中存在“NA/NaN/Inf (arg 1)”(不正确)。反正我试过了
kmeans(na.omit(sample_data), centers = 4)
基于here(和其他帖子)的答案,但没有奏效。我发现的唯一解决方法是使用
排除非数字列(即观察名称)kmeans(sample_data[, 2:5], centers = 4)
不幸的是,这使得集群的信息量大大减少,因为点现在有数字而不是名称。这是怎么回事?或者我怎样才能获得具有正确标签的聚类?
编辑:我正在尝试重现 this 过程/结果,但使用不同的数据集。请注意,当作者visualizes 聚类时,点会根据观察结果(在这种情况下为状态;或我的“obs1、obs2 等”)标记。
由于上述解决方法(删除带有观察名称的列),我得到了一系列数字标签。
代码和输入如下:
library(factoextra)
cluster <- kmeans(sample_data, centers = 4) #this doesn't work
cluster <- kmeans(sample_data[, 2:5], centers = 4) #this works
fviz_cluster(cluster, sample_data)
样本数据:
structure(list(name = structure(c(1L, 12L, 19L, 20L, 21L, 22L,
23L, 24L, 25L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 13L,
14L, 15L, 16L, 17L, 18L), .Label = c("obs1", "obs10", "obs11",
"obs12", "obs13", "obs14", "obs15", "obs16", "obs17", "obs18",
"obs19", "obs2", "obs20", "obs21", "obs22", "obs23", "obs24",
"obs25", "obs3", "obs4", "obs5", "obs6", "obs7", "obs8", "obs9"
), class = "factor"), variable1 = c(0, 0.383966783938484, 0.541654398529028,
0.469060314591266, 0.397636449124337, 0.3944696359856, 0.368740430902284,
0.998695171590958, 0.60013559365688, 0.543416096609665, 1, 0.287523586757021,
0.57818096701751, 0.504722587360754, 0.284825226469556, 0.295250085072615,
0.509782836343032, 0.392942062325636, 0.602608457169149, 0.474668174468815,
0.219951650206242, 0.263837738487209, 0.530976492805559, 0.312401708505963,
0.828799458392802), variable2 = c(0, 0.21094954480341, 0.374890541082605,
0.502470003202637, 0.385212751959443, 0.499052863381439, 0.172887314327707,
0.319869014605517, 0.484308813708282, 0.348608342250238, 0.474464311565186,
0.380406312920036, 1, 0.618253544624658, 0.560290273167607, 0.676315913606924,
0.339157532529115, 0.479005841710258, 0.576094917240369, 0.819742646967549,
0.472559283375261, 0.45594685111211, 0.160720270709769, 0.494360626922513,
0.658705091697224), variable3 = c(0, 0.0391726961740698, 0.157000498692027,
0.194883594782107, 0.133290754949737, 0.199085094994071, 0.000551185924636259,
0.418045152251051, 0.434858475480003, 0.443442199844268, 0.257231662911141,
0.195570389942169, 0.46503468971732, 0.358104620337886, 0.391852363829371,
0.39834809992812, 0.258870156344325, 0.38555892877453, 0.480559759927908,
1, 0.15662554228071, 0.279363773961277, 0.11211821625736, 0.180885222092932,
0.339650099009323), variable4 = c(0, 0.0464395032429444, 0.323768557597659,
0.201813172242373, 0.302710768912681, 0.446027132614423, 0.542018940773003,
1, 0.738123811706962, 0.550819613183929, 0.679555989322392, 0.563126171437818,
0.470328070009844, 0.316069092919459, 0.344421820993065, 0.222931758003036,
0.250406547916021, 0.381098780580988, 0.9526031202384, 0.174161621337361,
0.260548409706516, 0.288399563112687, 0.617089845066814, 0.265314653254406,
0.330637996311329)), class = "data.frame", row.names = c(NA,
-25L))
【问题讨论】:
-
什么意思,正确的标签?您可以使用
sample_data$cluster <- kmeans(sample_data[-1], centers = 4)$cluster将分配的集群添加到您的data.frame? -
将数据框的行名设置为第一列。
row.names(sample_data)<-sample_data[,1] -
@Axeman:喜欢这张图片:uc-r.github.io/public/images/analytics/clustering/kmeans/…;添加到问题的详细信息。取而代之的是'obs1,obs2等,'标记点,我只得到1-26的数字。我可以将集群分配绑定回原始数据框,但我不确定如何使用 fviz_cluster 使用更新的数据框来可视化距离/分组(它抱怨关于非数字数据的类似错误)。跨度>
-
@Dave2e:我得到了
Error in .rowNamesDF<-(x, value = value) : invalid 'row.names' length。
标签: r cluster-analysis k-means