Reputation: 21
I am try Elbow method to find the number of clusters in my data which was named as "data.clustering". It has to features: age and gender. The head of it as follow: head(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
My code to find k-clusters value on "data.clustering" data frame:
# 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);
As you can see in elbow.k function. I just return k value, but after I ran the snippet code above, the result have three k return as the same value is:
Time to find k using Elbow method is 38.39039 seconds with k value: 10 10 10
The expectation of my result is:
Time to find k using Elbow method is 38.39039 seconds with k value: 10
Can anyone help me to fix it ?
Upvotes: 1
Views: 412
Reputation: 812
I think your code work well. But you must edit the line code
k.clusters <- parSapply(cl, c(1:3), function(x) elbow.k(data.clustering));
to
k.clusters <- parSapply(cl, 1, function(x) elbow.k(data.clustering));
The second value will make the number of k return match with your expectation. It just simple mistake in your function.
Upvotes: 1