1

"data.clustering"という名前のデータでクラスタの数を検索するElbowメソッドを試しています。それには、年齢と性別があります。それの頭は以下の通りです: 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値を返しますが、私は上記のスニペットのコードを実行した後、結果は同じ値として3つの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 

缶誰私はそれを修正するために手伝ってください?

+0

途中でRの中で ';'を使う必要はありません。 –

+0

@Vincent Bonhommeもっと明確に説明できますか?ありがとうございます – Khongbich

+0

あなたの行の最後には、R言語で ';'は必要ありません。どのようにそれを明確にするか分からない;) –

答えて

1

あなたのコードはうまくいくと思います。しかし、あなたはあなたの期待にk個のリターンマッチの数を行います

k.clusters <- parSapply(cl, 1, function(x) elbow.k(data.clustering)); 

第2の値にラインコード

k.clusters <- parSapply(cl, c(1:3), function(x) elbow.k(data.clustering)); 

を編集する必要があります。それは単にあなたの機能の単純な間違いです。

+0

ご報告いただきありがとうございます。それは単なるパラメータの間違いです。 – Khongbich

関連する問題