2016-11-18 44 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个直方图的实际数据集,计算解决方案需要很长时间。同样,我想可能在其他具有多个变量直方图的数据集上花费太长时间。