Last active
August 29, 2015 14:15
-
-
Save shirayuca/6a9054acd609a89b7aa4 to your computer and use it in GitHub Desktop.
k-means.R
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
アイテム名 | 2392 | 2461 | 2739 | 3178 | |
---|---|---|---|---|---|
【アサヒ飲料】十六茶 | 0 | 0 | 0 | 0 | |
【伊藤園】お~いお茶 濃い味 | 0 | 0 | 0 | 0 | |
【伊藤園】お~いお茶 緑茶 | 0 | 0 | 0 | 0 | |
【キリン】生茶 | 0 | 0 | 0 | 0 | |
【コカ・コーラ】綾鷹 | 0 | 0 | 0 | 1 | |
【コカ・コーラ】爽健美茶 | 0 | 0 | 1 | 0 | |
【サントリー】伊右衛門 | 0 | 0 | 0 | 1 | |
【大塚製薬】ポカリスエット | 0 | 0 | 0 | 0 | |
【コカ・コーラ】コカ・コーラ | 1 | 1 | 0 | 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### k-means関数でクラスタリング | |
data <- read.csv("data.csv", sep=",", header=T, row.names=1) | |
data.sc <- scale(data) #データを標準化(各変量の単位が異なる場合) | |
km <- kmeans(data.sc, 10, iter.max = 10000, nstart=1) | |
#引数は、データ、クラスタ数、繰り返しの最大回数(デフォルトは N=10)、nstart(ランダムに初期値を設定する場合のパラメータ。初期値を変更したい場合に用いる)、algorithm(デフォルトは"Hartigan-Wong") | |
tapply(names(km$cluster), km$cluster, "unique") #クラスタ別に表示 | |
write.table(km$cluster, sep=",", "km.csv") #クラスタリング結果を出力 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hello