0

I created a dendrogram using

  1. AgglomerativeClustering
  2. dendrogram(linkage_x, labels=all_encoded, color_threshold=2.2)

And I obtain this outcome enter image description here

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 )```  
SFW
  • 31
  • 3
  • Does this answer your question? [How to Parse Data from SciKitLearn Agglomerative Clustering](https://stackoverflow.com/questions/65462806/how-to-parse-data-from-scikitlearn-agglomerative-clustering) – Robin Mackenzie Jul 03 '22 at 10:56
  • @RobinMackenzie, thanks! This answer is great for building the full tree but in my case, I'd like the JSON to not have the nodes 13, 11, 10 and place the nodes 4, 0, 3, 1, 2 as siblings (and same on the other branch of the tree). For my own dendrogram picture, I'd like a JSON that captures the branching in dark blue, but all the other clusters (colors) would be flattened (terminal nodes would be siblings). – SFW Jul 04 '22 at 22:36
  • Maybe a way to go is to create a different function that makes siblings (instead of recursive nesting) for any node in the link function that has less than 6 children per the linkage matrix (or a distance below my color_threshold of 2.2)? – SFW Jul 04 '22 at 23:13

0 Answers0