2016-06-09 6 views
-1

Sagen Sie für fünf Muster, mit Clustering-Methode, sie sind drei Klassen mit Label [1,1,2,1,3] zugeordnet, jetzt möchte ich eine 5 * 5 Assoziationsmatrix basierend darauf, wo in der Matrix 1 bedeutet, sie sind in der gleichen Cluster und 0 bedeutet sie sind in verschiedenen Clustern.Wie erstellt man eine Assoziationsmatrix basierend auf dem Clusterergebnis in Python?

Gibt es eine nette Implementierung in Python, um das zu tun? Da ich scikit-learn verwende, um das Clustering-Ergebnis zu generieren, bin ich mir nicht ganz sicher, ob einige Funktionen dafür verwendet werden können.

Antwort

0

Wenn ich richtig die gewünschte Ausgabe zu verstehen Sie die Matrix mit Liste Verständnis erzeugen:

>>> import pprint 
>>> l = [1,1,2,1,3] 
>>> res = [[int(x == y) for y in l] for x in l] 
>>> pprint.pprint(res) 
[[1, 1, 0, 1, 0], 
[1, 1, 0, 1, 0], 
[0, 0, 1, 0, 0], 
[1, 1, 0, 1, 0], 
[0, 0, 0, 0, 1]] 
0

Sie brauchen nicht eine Bibliothek dies zu tun.

Dies ist elementare Mathematik/Logik und kann in z.B. numpy.

Verwenden Sie den Etikettenvektor, wiederholen Sie es in quadratische Form und es ist rep == rep.transpose() ist Ihre Assoziationsmatrix.

Dies in numpy zu tun ist effizienter als eine reine Python-Lösung mit list-of-lists-of-integer-Objekten.

0

Wenn Sie versuchen, eine Assoziationsmatrix als Teil eines Abstimmungsmechanismus zu bauen, können Sie @niemmi noch weiter beantworten erarbeiten:

#Declare the weight of each vote 
vote = 1/len(estimators) 
#co_association matrix is 5X5 (5 patterns) 
co_association = np.zeros((5, 5)) 

#for each of your estimators 
for est in estimators: 
    #fit the data and grab the labels 
    est.fit(data) 
    labels = est.labels_ 
    #find all associations and transform it into a numpy array 
    res = [[int(i == j) for i in labels] for j in labels] 
    res = np.array(res) 
    #Vote and update the co_association matriz 
    res = res * vote 
    co_association = co_association + res 
Verwandte Themen