2016-11-18 3 views
1

Ich habe Daten als eine Anzahl von verschiedenen Histogrammen einer einzelnen Variablen dargestellt. Ich möchte herausfinden, welche Histogramme ähnlich sind, indem Sie unüberwachtes Clustering verwenden. Ich möchte auch die optimale Anzahl der zu verwendenden Cluster kennen.Cluster-Histogramme mit Earth Movern Entfernung R

Ich habe gelesen, über die Earth-Movers-Distanz-Metrik als ein Maß für die Entfernung zwischen Histogrammen, aber nicht wissen, wie das in gemeinsamen Clustering-Algorithmen zu verwenden (z. B. k bedeutet).

Primär: Welche Pakete und Funktionen verwende ich zum Clusterhistogramm?

Sekundär: Wie ermittle ich die "optimale" Anzahl von Clustern?

Beispiel Datensatz 1 (3 uni-modal Cluster):

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 

Beispiel Datensatz 2 (3 bimodale Cluster):

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 ist sehr teuer, daher müssen Sie untere Grenzen und Indizes verwenden, um das Clustering zu beschleunigen. K-means funktioniert nur für Bregman-Divergenzen, und ich glaube nicht, dass EMD eins ist. –

Antwort

1

Clusterlösung für Beispiel Datensatz 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 

Clusterlösung für Beispieldatensatz 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 

Bestimmung „Optimal“ Anzahl der Cluster:

Ich bin mir nicht sicher, was die beste Metrik aber dieses Paket spuckt ein quality Metrik in der Ausgabe sein würde. Obwohl es ineffizient ist, mehrere Lösungen zu berechnen und sie dann zu bewerten, war dies meine erste Lösung.

Optimal-Cluster für Beispiel Datensatz 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

Der Plot zeigt eine deutliche Zunahme der "Qualität" zwischen 2 und Cluster 3 Cluster und einem geringen Verbesserung von über 3 Clustern. Also wähle ich 3 als "optimal". Dies ist sinnvoll, da ich das Rohdatenbeispiel speziell für 3 Cluster erstellt habe.

Zum Beispiel 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

Wieder einmal der größte Anstieg in quality von 2 Cluster 3 Cluster.

Hat jemand Alternativen vorgeschlagen? Dies erfordert eine sehr lange Zeit, um Lösungen auf meinem tatsächlichen Datensatz von über 2500 Histogrammen zu berechnen. Ähnlich würde ich mir vorstellen, dass es bei anderen Datensätzen, die Histogramme für mehrere Variablen haben, viel zu lange dauern könnte.

Verwandte Themen