GuyAllard / markov_clustering

markov clustering in python
MIT License
168 stars 37 forks source link

Network Graph from SciPy Hierarchical Clustering #14

Closed rohithth closed 5 years ago

rohithth commented 5 years ago

Hi,

Using SciPy Hierarchical Clustering how can we create the network graph?

from matplotlib import pyplot as plt from scipy.cluster.hierarchy import dendrogram, linkage import numpy as np

generate two clusters: a with 100 points, b with 50:

np.random.seed(4711) # for repeatability of this tutorial a = np.random.multivariate_normal([10, 0], [[3, 1], [1, 4]], size=[100,]) b = np.random.multivariate_normal([0, 20], [[3, 1], [1, 4]], size=[50,]) X = np.concatenate((a, b),) print X.shape # 150 samples with 2 dimensions plt.scatter(X[:,0], X[:,1]) plt.show()

generate the linkage matrix

Z = linkage(X, 'ward')

from scipy.cluster.hierarchy import cophenet from scipy.spatial.distance import pdist

c, coph_dists = cophenet(Z, pdist(X)) c

calculate full dendrogram

plt.figure(figsize=(25, 10)) plt.title('Hierarchical Clustering Dendrogram') plt.xlabel('sample index') plt.ylabel('distance') dendrogram( Z, leaf_rotation=90., # rotates the x axis labels leaf_font_size=8., # font size for the x axis labels ) plt.show()

Now how do we plot the network graph like yours instead of dendrogram? Kindly assist. Thank you!

GuyAllard commented 5 years ago

Hi,

I think that this question would be more suitable for the NetworkX or SciPy discussion groups.

I am closing this issue as not relevant to this project.