我正在尝试将scipy分层聚类的结果转换为json以在d3.js中显示,此处为示例
以下代码生成具有6个分支的树状图。
import pandas as pd
import scipy.spatial
import 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
我的问题我如何以d3.js理解的格式表示json文件中的数据
{'name': 'Root1’,
'children':[{'name' : 'B'},
{'name': 'E-D-F-C-A',
'children' : [{'name': 'C-A',
'children' : {'name': 'A'},
{'name' : 'C'}]
}
}
]
}
令人尴尬的事实是,我不知道我是否可以从树状图或链接矩阵中提取此信息,以及如何
我很感谢我能得到的任何帮助。
编辑以澄清
到目前为止,我尝试使用totree方法,但是在理解其结构方面遇到困难(是的,我阅读了文档)。
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()
您可以通过三个步骤执行此操作:
构造一个表示树状图的嵌套字典
对于第一步,重要的是使用进行调用to_tree
,rd=False
以便返回树状图的根。从该根目录,您可以按以下方式构造嵌套字典:
# Create a nested dictionary from the ClusterNode's returned by SciPy
def add_node(node, parent ):
# First create the new node and append it to its parent's children
newNode = dict( node_id=node.id, children=[] )
parent["children"].append( newNode )
# Recursively add the current node's children
if node.left: add_node( node.left, newNode )
if node.right: add_node( node.right, newNode )
T = scipy.cluster.hierarchy.to_tree( clusters , rd=False )
d3Dendro = dict(children=[], name="Root1")
add_node( T, d3Dendro )
# Output: => {'name': 'Root1', 'children': [{'node_id': 10, 'children': [{'node_id': 1, 'children': []}, {'node_id': 9, 'children': [{'node_id': 6, 'children': [{'node_id': 0, 'children': []}, {'node_id': 2, 'children': []}]}, {'node_id': 8, 'children': [{'node_id': 5, 'children': []}, {'node_id': 7, 'children': [{'node_id': 3, 'children': []}, {'node_id': 4, 'children': []}]}]}]}]}]}
基本思想是从不在树状图中的节点开始,该节点将作为整个树状图的根。然后我们递归地将左儿童和右儿童添加到该字典中,直到到达叶子为止。在这一点上,我们没有节点的标签,因此我只是通过节点的clusterNode ID来标记节点。
标记树状图
接下来,我们需要使用node_ids来标记树状图。评论应足以解释其工作原理。
# Label each node with the names of each leaf in its subtree
def label_tree( n ):
# If the node is a leaf, then we have its name
if len(n["children"]) == 0:
leafNames = [ id2name[n["node_id"]] ]
# If not, flatten all the leaves in the node's subtree
else:
leafNames = reduce(lambda ls, c: ls + label_tree(c), n["children"], [])
# Delete the node id since we don't need it anymore and
# it makes for cleaner JSON
del n["node_id"]
# Labeling convention: "-"-separated leaf names
n["name"] = name = "-".join(sorted(map(str, leafNames)))
return leafNames
label_tree( d3Dendro["children"][0] )
转储为JSON并加载到D3中
最后,在树状图被标记后,我们只需要将其输出到JSON并加载到D3中即可。我只是粘贴Python代码以将其转储到JSON,以确保完整性。
# Output to JSON
json.dump(d3Dendro, open("d3-dendrogram.json", "w"), sort_keys=True, indent=4)
输出量
我在下面创建了树状图的Scipy和D3版本。对于D3版本,我只需将输出('d3-dendrogram.json'
)的JSON文件插入此Gist即可。
科学树状图
D3树状图
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句