kmeans抱怨“什么时候没有NA / NaN / Inf调用外部函数(arg 1)”?

时间:2018-11-30 19:43:28

标签: r cluster-analysis k-means

我正在尝试在相对简单的数据帧上进行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)

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))

2 个答案:

答案 0 :(得分:0)

K-均值适用于连续变量。

它可能试图将标签转换为数字,但这没有用。

从不在分析中不包括标识符列!

正确的数据预处理至关重要,并且需要90%的工作;您需要准确了解需求。仅使其以某种方式运行是不够的-使其易于运行,但是会返回无用的结果...

答案 1 :(得分:0)

关键是使用

将具有所需标签的列转换为行名

df <- tibble::column_to_rownames(df, var = "labels")

这样,聚类算法甚至不会考虑标签,但会将其应用于聚类上的点。