2016-11-18 69 views
1

我將數據表示爲單個變量的許多不同直方圖。我想確定使用無監督聚類的哪些直方圖是相似的。我也想知道使用的最佳羣集數量。使用地球移動距離的聚類直方圖距離R

我已閱讀Earth Movers Distance度量作爲度量直方圖之間距離的度量,但不知道如何在通用聚類算法中使用該距離度量(例如,k均值)。

主要:我用什麼r軟件包和函數來聚合直方圖?

中學:如何確定「最佳」數量的聚類?

實施例數據集1(3單峯簇):

v1 <- rnorm(n=100, mean = 10, sd = 1) # cluster 1 (around 10) 
v2 <- rnorm(n=100, mean = 50, sd = 5) # cluster 2 (around 50) 
v3 <- rnorm(n=100, mean = 100, sd = 10) # cluster 3 (around 100) 
v4 <- rnorm(n=100, mean = 12, sd = 2) # cluster 1 
v5 <- rnorm(n=100, mean = 45, sd = 6) # cluster 2 
v6 <- rnorm(n=100, mean = 95, sd = 6) # cluster 3 

實施例數據集2(3雙峯簇):

b1 <- c(rnorm(n=100, mean=9, sd=2) , rnorm(n=100, mean=200, sd=20)) # cluster 1 (around 10 and 200) 
b2 <- c(rnorm(n=100, mean=50, sd=5), rnorm(n=100, mean=100, sd=10)) # cluster 2 (around 50 and 100) 
b3 <- c(rnorm(n=100, mean=99, sd=8), rnorm(n=100, mean=175, sd=17)) # cluster 3 (around 100 and 175) 
b4 <- c(rnorm(n=100, mean=12, sd=2), rnorm(n=100, mean=180, sd=40)) # cluster 1 
b5 <- c(rnorm(n=100, mean=45, sd=6), rnorm(n=100, mean=80, sd=30)) # cluster 2 
b6 <- c(rnorm(n=100, mean=95, sd=6), rnorm(n=100, mean=170, sd=25)) # cluster 3 
b7 <- c(rnorm(n=100, mean=10, sd=1), rnorm(n=100, mean=210, sd=30)) # cluster 1 (around 10 and 200) 
b8 <- c(rnorm(n=100, mean=55, sd=5), rnorm(n=100, mean=90, sd=15)) # cluster 2 (around 50 and 100) 
b9 <- c(rnorm(n=100, mean=89, sd=9), rnorm(n=100, mean=165, sd=20)) # cluster 3 (around 100 and 175) 
b10 <- c(rnorm(n=100, mean=8, sd=2), rnorm(n=100, mean=160, sd=30)) # cluster 1 
b11 <- c(rnorm(n=100, mean=55, sd=6), rnorm(n=100, mean=110, sd=10)) # cluster 2 
b12 <- c(rnorm(n=100, mean=105, sd=6), rnorm(n=100, mean=185, sd=21)) # cluster 3 
+0

EMD非常昂貴,所以您需要使用下界和索引來加速您的羣集。 K-means只適用於Bregman分歧,我不認爲EMD是其中之一。 –

回答

1

聚類解實施例數據集1:

library(HistDAWass) 

# create lists of histogram distributions 
lod<-vector("list",6) 
lod[[1]] <- data2hist(v1, type = "regular") 
lod[[2]] <- data2hist(v2, type = "regular") 
lod[[3]] <- data2hist(v3, type = "regular") 
lod[[4]] <- data2hist(v4, type = "regular") 
lod[[5]] <- data2hist(v5, type = "regular") 
lod[[6]] <- data2hist(v6, type = "regular") 

# combine separate lists into a matrix of histogram objects 
mymat <- new("MatH", nrows=6, ncols=1, ListOfDist=lod, names.rows=c(1:6), names.cols="density") 

# calculate clusters pre-specifying number of clusters (k) 
WH_kmeans(mymat, k=3) 

# the output of this gives the expected 3 clusters 

示例數據集2的聚類解決方案:

lod<-vector("list",12) 
lod[[1]] <- data2hist(b1, type = "regular") 
lod[[2]] <- data2hist(b2, type = "regular") 
lod[[3]] <- data2hist(b3, type = "regular") 
lod[[4]] <- data2hist(b4, type = "regular") 
lod[[5]] <- data2hist(b5, type = "regular") 
lod[[6]] <- data2hist(b6, type = "regular") 
lod[[7]] <- data2hist(b7, type = "regular") 
lod[[8]] <- data2hist(b8, type = "regular") 
lod[[9]] <- data2hist(b9, type = "regular") 
lod[[10]] <- data2hist(b10, type = "regular") 
lod[[11]] <- data2hist(b11, type = "regular") 
lod[[12]] <- data2hist(b12, type = "regular") 

mymat2 <- new("MatH", nrows=12, ncols=1, ListOfDist=lod, names.rows=c(1:12), names.cols="density") 

WH_kmeans(mymat2, k=3) 

# the output of this also gives the expected 3 clusters 

確定「最佳」簇數:

我不知道最好的指標是什麼,但這個包吐出quality輸出指標。因此,雖然計算幾個解決方案並評估它們效率不高,但使用這是我的初始解決方案。

實施例數據集1最優簇:

df = data.frame() 
for(i in 2:5) { 
    df = rbind(df, data.frame(n_clust = i, quality = WH_kmeans(mymat, k=i)$quality)) 
} 

ggplot(df, aes(x=n_clust, y=quality)) + geom_point(size=4) + geom_line() 

Example 1 Optimality plot

該圖顯示在2簇和簇3和上述3個集羣稍加改進之間的「質量」的明顯增加。所以,我選擇3作爲「最優」。這是有道理的,因爲我特別創建了原始數據示例以擁有3個羣集。

例如2:

df2 = data.frame() 
for(i in 2:11) { 
    df2 = rbind(df2, data.frame(n_clust = i, quality = WH_kmeans(mymat2, k=i)$quality)) 
    # this loop errors out after k=6 for me but the answer is already clear. 
} 

ggplot(df2) + geom_line(aes(x=n_clust, y=quality)) 

Example 2 Optimality Plot

再次在quality增幅最大爲2簇到3簇。

任何人都有建議的替代品?對於超過2500個直方圖的實際數據集,計算解決方案需要很長時間。同樣,我想可能在其他具有多個變量直方圖的數據集上花費太長時間。