I created a dendrogram using
- AgglomerativeClustering
- dendrogram(linkage_x, labels=all_encoded, color_threshold=2.2)
I'd like to turn this into JSON to then visualize using d3.tree().
The format I am looking is to capture the hierarchical structure in dark blue while having the nodes in each colored clusters at the same level (instead of being split in 2 up to the leaves)
Is there an existing function for doing that?
Otherwise I've seen this code for building the full dendrogram but I am not sure how to adapt it to not exhaustively reproduce the dendrogram... Thanks for any pointers!
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 )```