web-dev-qa-db-ja.com

K平均法:ロイド、フォージー、マックイーン、ハーティガンウォン

私はRでK平均アルゴリズムを使用しています。統計パッケージの関数「kmeans」で使用できる4つのアルゴリズム、Lloyd、Forgy、MacQueen、Hartigan-Wongの違いを理解したいと思います。

しかし、私はこの質問に対して十分な答えを得ることは注目に値しました。

まれな情報しか見つかりませんでした:( http://en.wikibooks.org/wiki/Data_Mining_Algorithms_In_R/Clustering/K-Means にアクセスしてください)

この説明から、ロイド、フォージー、ハーティガン・ウォンは私には同じように見えます。二乗和の最小化またはユークリッド距離の最小化は同じです。

MacQueenは、オブジェクトが別のクラスターに移動された場合、関連する2つのクラスターを更新するという点で異なります。

それにもかかわらず、これらのアルゴリズムがどの点で異なるかはまだわかりません。

24
user2974776

[〜#〜] r [〜#〜]は、ロイドのアルゴリズムをkmeans();のオプションとして提供します。 Hartigan and Wong(1979)によるデフォルトのアルゴリズムは、はるかにスマートです。 MacQueenのアルゴリズム(MacQueen、1967)と同様に、ポイントが移動するたびに図心を更新します。また、最も近いクラスターをチェックする際に賢い(時間節約)選択を行います。一方、ロイドのk平均アルゴリズムは、これらすべてのクラスタリングアルゴリズムの最初で最も単純なものです。

Lloydのアルゴリズム(Lloyd、1957)は、一連の観測値またはケース(nxp行列の行、またはRealsの点と考えてください)を取り、それらをkグループにクラスター化します。クラスタ内の二乗和を最小化しようとします enter image description here

ここで、u_iはクラスターS_i内のすべての点の平均です。アルゴリズムは次のように進行します(網羅的な表記法の正式さは省略します)。 enter image description here

ただし、Rの実装には問題があり、複数の開始点を検討すると問題が発生します。アルゴリズムは収束することが保証されていますが、グローバルオプティマをカバーすることが保証されていないため、一般的にいくつかの異なる出発点を考慮することは賢明です。これは、大規模で高次元の問題に特に当てはまります。簡単な例から始めましょう(大きな、特に難しい)。

(ここでは、ラテックスで数式を書くことができないので、いくつかの画像を貼り付けます)

enter image description hereenter image description hereenter image description hereenter image description here

クラスターの順序は任意ですが、解決策は以前に達成されたものと非常に似ています。さらに重要なことに、このジョブは0.199秒しかかかりませんでした。確かに、これは本当であるにはあまりにも良いです。3つのプロセッサコアを使用すると、せいぜい、最初の(順次)実行の3分の1の時間がかかるはずです。これは問題ですか?無料ランチのようですね。たまに無料ランチで問題ないですよね?

enter image description here

これは常にR関数で機能するとは限りませんが、コードを直接見る機会がある場合があります。これはその時代の1つです。このコードをファイルmykmeans.Rに入れ、手動で編集して、さまざまな場所にcat()ステートメントを挿入します。これは、sink()を使用してこれを行う賢い方法です(これはSweaveでは機能しないようですが、インタラクティブに機能します)。

> sink("mykmeans.R")
> kmeans
> sink()

ファイルを編集して、関数名を変更し、cat()ステートメントを追加します。末尾の行も削除する必要があることに注意してください::

enter image description here

その後、探索を繰り返すことができますが、mykmeans()を使用します。

> source("mykmeans.R")
> start.kmeans <- proc.time()[3]
> ans.kmeans <- mykmeans(x, 4, nstart = 3, iter.max = 10, algorithm = "Lloyd")
JJJ statement 1: 0 elapsed time.
JJJ statement 5: 2.424 elapsed time.
JJJ statement 6: 2.425 elapsed time.
JJJ statement 7: 2.52 elapsed time.
JJJ statement 6: 2.52 elapsed time.
JJJ statement 7: 2.563 elapsed time.

enter image description here

現在、私たちはビジネスに取り組んでいます。ほとんどの時間はステートメント5の前に消費されていました(もちろん、これはわかっていたため、ステートメント5が2ではなく5でした)...

ここにコードがあります:

#######################################################################
# kmeans()

N <- 100000
x <- matrix(0, N, 2)
x[seq(1,N,by=4),] <- rnorm(N/2)
x[seq(2,N,by=4),] <- rnorm(N/2, 3, 1)
x[seq(3,N,by=4),] <- rnorm(N/2, -3, 1)
x[seq(4,N,by=4),1] <- rnorm(N/4, 2, 1)
x[seq(4,N,by=4),2] <- rnorm(N/4, -2.5, 1)
start.kmeans <- proc.time()[3]
ans.kmeans <- kmeans(x, 4, nstart=3, iter.max=10, algorithm="Lloyd")
ans.kmeans$centers
end.kmeans <- proc.time()[3]
end.kmeans - start.kmeans

these <- sample(1:nrow(x), 10000)
plot(x[these,1], x[these,2], pch=".")
points(ans.kmeans$centers, pch=19, cex=2, col=1:4)

library(foreach)
library(doMC)
registerDoMC(3)
start.kmeans <- proc.time()[3]
ans.kmeans.par <- foreach(i=1:3) %dopar% {
  return(kmeans(x, 4, nstart=1, iter.max=10, algorithm="Lloyd"))
}
TSS <- sapply(ans.kmeans.par, function(a) return(sum(a$withinss)))
ans.kmeans.par <- ans.kmeans.par[[which.min(TSS)]]
ans.kmeans.par$centers
end.kmeans <- proc.time()[3]
end.kmeans - start.kmeans

sink("mykmeans.Rfake")
kmeans
sink()

source("mykmeans.R")
start.kmeans <- proc.time()[3]
ans.kmeans <- mykmeans(x, 4, nstart=3, iter.max=10, algorithm="Lloyd")
ans.kmeans$centers
end.kmeans <- proc.time()[3]
end.kmeans - start.kmeans

#######################################################################
# Diving

x <- read.csv("Diving2000.csv", header=TRUE, as.is=TRUE)
library(YaleToolkit)
whatis(x)

x[1:14,c(3,6:9)]

meancol <- function(scores) {
  temp <- matrix(scores, length(scores)/7, ncol=7)
  means <- apply(temp, 1, mean)
  ans <- rep(means,7)
  return(ans)
}
x$panelmean <- meancol(x$JScore)

x[1:14,c(3,6:9,11)]

meancol <- function(scores) {
  browser()
  temp <- matrix(scores, length(scores)/7, ncol=7)
  means <- apply(temp, 1, mean)
  ans <- rep(means,7)
  return(ans)
}

x$panelmean <- meancol(x$JScore)

ここに説明があります:

Number of cases: 10,787 scores from 1,541 dives (7 judges score each
dive) performed in four events at the 2000 Olympic Games in Sydney,
Australia.

Number of variables: 10.

Description: A full description and analysis is available in an
article in The American Statistician (publication details to be
announced).

Variables:

Event       Four events, men's and women's 3M and 10m.
Round       Preliminary, semifinal, and final rounds.
Diver       The name of the diver.
Country     The country of the diver.
Rank        The final rank of the diver in the event.
DiveNo      The number of the dive in sequence within round.
Difficulty  The degree of difficulty of the dive.
JScore      The score provided for the judge on this dive.
Judge       The name of the judge.
JCountry    The country of the judge.

そして、それを試すデータセット https://www.dropbox.com/s/urgzagv0a22114n/Diving2000.csv

25
cMinor

16ページに移動-
これはGorgy/Llyods、MacQueen、Hatigan-Wongアルゴスの明確な説明があります。

https://core.ac.uk/download/pdf/27210461.pdf

0
Arpit Sisodia