【发布时间】:2016-06-11 16:03:30
【问题描述】:
我正在尝试使用 Elbow 方法来查找我的数据中名为“data.clustering”的集群数量。它具有特征:年龄和性别。它的头部如下: 头(数据。集群);
> head(data.clustering)
age gender
2 2 1
3 6 2
4 2 1
5 2 1
6 6 2
7 6 1
我在“data.clustering”数据框上查找 k-clusters 值的代码:
# include library
require(stats)
library(GMD)
library(ggplot2)
library(parallel)
# include function
source('~/Workspaces/Projects/RProject/MovielensCluster/readData.R');
###
elbow.k <- function(mydata){
## determine a "good" k using elbow
dist.obj <- dist(mydata);
hclust.obj <- hclust(dist.obj);
css.obj <- css.hclust(dist.obj,hclust.obj);
elbow.obj <- elbow.batch(css.obj);
# print(elbow.obj)
k <- elbow.obj$k
return(k)
}
# include file
filePath <- "dataset/u.user";
data.original <- readtext.tocsv(filePath);
data.convert <- readtext.convert(filePath);
data.clustering <- data.convert[,c(-1,-4)];
# find k value
no_cores <- detectCores();
cl<-makeCluster(no_cores);
clusterEvalQ(cl, library(GMD));
clusterExport(cl, list("data.clustering", "data.original", "elbow.k", "clustering.kmeans"));
start.time <- Sys.time();
k.clusters <- parSapply(cl, c(1:3), function(x) elbow.k(data.clustering));
end.time <- Sys.time();
cat('Time to find k using Elbow method is',(end.time - start.time),'seconds with k value:', k.clusters);
正如您在肘部.k 函数中看到的那样。我只是返回k值,但是在我运行上面的sn-p代码之后,结果有三个k返回,因为相同的值是:
Time to find k using Elbow method is 38.39039 seconds with k value: 10 10 10
我对结果的期望是:
Time to find k using Elbow method is 38.39039 seconds with k value: 10
谁能帮我解决它?
【问题讨论】:
-
顺便说一句,R 中不需要
; -
@Vincent Bonhomme 你能更清楚地解释我 - 为什么?非常感谢
-
你不需要
;在你的行尾,用 R 语言。不知道如何使它更清楚;) -
是的。谢谢 !但我认为,当你不使用 ';'在功能上。它将在 R 控制台上打印值。不舒服
-
不,不会的。试试看。
标签: r machine-learning parallel-processing cluster-analysis