Chadrick Blog

AgglomerativeClustering by distance example

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

test\_values = \[20, 14, 19, 19, 19, 18, 17, 19, 19, 103, 105, 84, 83, 108, 75, 80, 92, 78\]

x = \[\[i\] for i in test\_values\]

Z = linkage(x, method='ward')

plt.figure()
dendrogram(Z)
plt.show()

clusters = fcluster(Z, 10, criterion='distance')

cluster\_result={}

for index, cluster\_id in enumerate(clusters):
    val = test\_values\[index\]
    fetch\_list = cluster\_result.get(cluster\_id,None)

    if fetch\_list is None:
        fetch\_list=\[val\]
        cluster\_result\[cluster\_id\] = fetch\_list
    else:
        fetch\_list.append(val)

print(cluster\_result)