from sklearn.cluster import KMeans # Perform k-means clustering with four clustersclustering = KMeans(n_clusters=4, random_state=0).fit(correlation_mat) # Print the cluster labelscluster_labels=clustering.labels_print_clusters(df_combined,cluster_labels)
defplot_cluster_heatmaps(cluster_results, companies):""" Plots the heatmaps of clustering for all companies for different methods side by side. Args: - cluster_results: a dictionary of cluster labels for each clustering method - companies: a list of company names - 建议关注@公众号:数据STUDIO 定时推送更多优质内容 """# 从字典中提取key和value methods = list(cluster_results.keys()) labels = list(cluster_results.values()) # 定义每个方法的热图数据 heatmaps = []for i in range(len(methods)): heatmap = np.zeros((len(np.unique(labels[i])), len(companies)))for j in range(len(companies)): heatmap[labels[i][j], j] = 1 heatmaps.append(heatmap) # Plot the heatmaps in a 2x2 grid fig, axs = plt.subplots(nrows=2, ncols=2, figsize=(12, 12)) for i in range(len(methods)): row = i // 2 col = i % 2 sns.heatmap(heatmaps[i], cmap="Blues", annot=True, fmt="g", xticklabels=companies, ax=axs[row, col]) axs[row, col].set_title(methods[i]) plt.tight_layout() plt.show() companies=df_combined.columnsplot_cluster_heatmaps(cluster_results, companies)