1
我嘗試使用Elbow方法來查找我的數據中被命名爲「data.clustering」的羣集數量。它的特點是:年齡和性別。它的頭像如下: head(data.clustering);使用Elbow方法返回多個值以查找羣集的數量
> 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-集羣值:
# 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);
正如你可以elbow.k功能看。我剛剛返回的k值,但之後我跑上面的代碼片段,結果有幾種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你能更清楚地解釋我 - 爲什麼?非常感謝 – Khongbich
,您不需要在R語言結尾處使用';'。不知道如何使它更清晰;) –