[SciPy-User] scipy.cluster : linkagematrix to tree
almugabo at googlemail.com
almugabo at googlemail.com
Thu Nov 14 01:17:36 EST 2013
(I posted this to stackoverflow but a friend thought that this is best place to look for help for scipy specific issue)
I am struggling to transform results of a hierarchical clusterring in a json format
Here my code :
import pandas as pd import scipy.spatialimport scipy.cluster
d = {'employee' : ['A', 'B', 'C', 'D', 'E', 'F'],
'skillX': [2,8,3,6,8,10],
'skillY': [8,15,6,9,7,10]}
d1 = pd.DataFrame(d)
distMat = xPairWiseDist = scipy.spatial.distance.pdist(np.array(d1[['skillX', 'skillY']]), 'euclidean')
clusters = scipy.cluster.hierarchy.linkage(distMat, method='single')
dendo = scipy.cluster.hierarchy.dendrogram(clusters, labels = list(d1.employee), orientation = 'right')
dendo
my question :
how to transform the linkage matrix in json format that d3.js understands
{'name': 'Root1’,
'children':[{'name' : 'B'},
{'name': 'E-D-F-C-A',
'children' : [{'name': 'C-A',
'children' : {'name': 'A'},
{'name' : 'C'}]
}
}
]}
I understand this is what the totree method is for but could not find any example which will help me to understand it (despite the excellent documentation).
I get so far
a = scipy.cluster.hierarchy.to_tree(clusters , rd=True)for x in a[1]:
#print x.get_id()
if x.is_leaf() != True :
print x.get_left().get_id(), x.get_right().get_id(), x.get_count()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.scipy.org/pipermail/scipy-user/attachments/20131114/40808057/attachment.html>
More information about the SciPy-User
mailing list