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!
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!