from scipy.cluster.hierarchy import dendrogram, linkage from sklearn.cluster import AgglomerativeClustering from scipy.cluster.hierarchy import fcluster #create distance matrix linkage_data = linkage(data, method = 'ward', metric = 'euclidean', optimal_ordering = True) #view dendrogram dendrogram(linkage_data) plt.title('Hierarchical Clustering Dendrogram') plt.xlabel('Data point') plt.ylabel('Distance') plt.show() #assign depth and clusters clusters = fcluster(linkage_data, 2.5, criterion = 'inconsistent', depth = 5)