聚类结果打印 python 中的详细信息

Clustering results print with details in python

我只是尝试在 python(二维数组 Numpy)中打印我的聚类结果。但是我找不到任何关于打印结果的解决方案。

我绘制了树状图,但我需要结果,例如:

集群 1:

第 2 组:

第 3 组:

我的代码是:

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.

如果您在

阅读文档

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

你会注意到那里的第一个方法:

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

你想要扁平个集群。