How to cross a tree from sklearn AgglomerativeClustering?

I have an array of numpy text files at: https://github.com/alvations/anythingyouwant/blob/master/WN_food.matrix

This is the distance matrix between the terms and each other, my list of terms is as follows: http://pastebin.com/2xGt7Xjh

I used the following code to create a hierarchical cluster:

import numpy as np
from sklearn.cluster import AgglomerativeClustering

matrix = np.loadtxt('WN_food.matrix')
n_clusters = 518
model = AgglomerativeClustering(n_clusters=n_clusters,
                                linkage="average", affinity="cosine")
model.fit(matrix)

To get clusters for each term, I could do:

for term, clusterid in enumerate(model.labels_):
    print term, clusterid

But how do I cross the tree that produces AgglomerativeClustering?

Is it possible to convert it to scipy dendrogram ( http://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.cluster.hierarchy.dendrogram.html )? And after that, how do I go through the dendrogram?

+4
1

sklearn.cluster.ward_tree: sklearn.cluster.ward_tree?

AgglomerativeClustering , children_. AgglomerativeClustering. (node_id, left_child, right_child) node .

import numpy as np
from sklearn.cluster import AgglomerativeClustering
import itertools

X = np.concatenate([np.random.randn(3, 10), np.random.randn(2, 10) + 100])
model = AgglomerativeClustering(linkage="average", affinity="cosine")
model.fit(X)

ii = itertools.count(X.shape[0])
[{'node_id': next(ii), 'left': x[0], 'right':x[1]} for x in model.children_]

fooobar.com/questions/698191/...

+13

All Articles