9

How to visualize a hierarchical clustering as a tree of labelled nodes in Python...

 2 years ago
source link: https://datascience.stackexchange.com/questions/101854/how-to-visualize-a-hierarchical-clustering-as-a-tree-of-labelled-nodes-in-python/101949#101949
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

The chapter "Normalized Information Distance", visualizes a hierarchical clustering as a tree of nodes with labels:

Unfortunately I cannot find out how to replicate this visualization, maybe they did it in a manual way with Tikz? How can I achieve this effect automatically in Python, preferably with Scikit-Learn? I only found the Dendogram, which looks nothing like the effect I want to replicate:

Result (thanks at @andy-w):

model = AgglomerativeClustering(linkage="average", n_clusters=N_CLUSTERS, compute_distances=True, affinity="l1")
model.fit(data)
no_of_observations = np.arange(2, model.children_.shape[0]+2)
linkage_matrix = np.column_stack([model.children_, model.distances_, no_of_observations]).astype(float)

G = nx.Graph()    
n = len(linkage_matrix)
for i in range(n):
    row = linkage_matrix[i]
    G.add_edge(label(int(row[0])),label(n+i+1),len=1+0.1*(math.log(1+row[2])))  
    G.add_edge(label(int(row[1])),label(n+i+1),len=1+0.1*(math.log(1+row[2])))  

dot = nx.nx_pydot.to_pydot(G).to_string()
dot = graphviz.Source(dot, engine='neato')
dot.render(format='pdf',filename='tree')



About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK