Search code examples
pythonnumpyscipycluster-analysishierarchical-clustering

Clustering results print with details in python


I just try to print my clustering results in python (2D array Numpy). But I can't find any solution about printing results.

I draw my dendrogram but I need results for example:

Cluster 1:

Cluster 2:

Cluster 3:

My code is:

from matplotlib import pyplot as plt
from scipy.cluster.hierarchy import dendrogram, linkage
from scipy.spatial.distance import pdist as dx
import scipy.cluster.hierarchy as ch
import scipy.spatial.distance as dx
import csv
import numpy as np

if __name__ == "__main__":

from numpy import genfromtxt

df = genfromtxt('Booking.csv', delimiter=',')

tridf=np.triu(df)
overdf=Z = linkage(tridf,'complete','chebychev')
plt.figure(figsize=(25, 10))
plt.title('Dendrogram')
plt.xlabel('Row No')
plt.ylabel('Dist')


dendrogram(
    Z,
     truncate_mode='level',
     show_leaf_counts=True,
     show_contracted=True,
    leaf_rotation=90.,  # rotates the x axis labels
    leaf_font_size=10.,  # font size for the x axis labels
)

plt.show()
plt.savefig('Dendogram.jpg')

with open("outputZ.csv", "wb") as f:
writer = csv.writer(f)
writer.writerows(Z) # I just want to print clustering results to csv file  but it comes meaningless numbers.

Solution

  • If you read the documentation at

    http://docs.scipy.org/doc/scipy/reference/cluster.hierarchy.html

    You will notice the first method there:

    fcluster(Z, t[, criterion, depth, R, monocrit]) Forms flat clusters from the hierarchical clustering defined by the linkage matrix Z.

    You want flat clusters.