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)

0 Comments

Leave a Reply

Your email address will not be published.