简体   繁体   English

如何将 label 放入聚类绘制图像中:在侧面指示它是哪个聚类,并在中心指示?

[英]How to put label in Clustering plotted image : indicating in the side which cluster it is, and also in the center?

i used k means clustering and would like to put label both in the side indicating the colors and in the center telling which cluster centroid it is, Can anybody please help?我使用 k 表示聚类,并想将 label 放在指示 colors 的一侧,并在中心告诉它是哪个聚类质心,有人可以帮忙吗? i tried the solutions, but it didnt help.我尝试了解决方案,但没有帮助。 Thank you.谢谢你。 My code is as folows:我的代码如下:

i also tried to map the name but it didnt work well.我也试过 map 这个名字,但效果不佳。 # map clusters to appropriate labels cluster_map = {0: "0", 1: "1", 2: "2", 3:"3", 4:"4", 5: "5"} # apply mapping df['cluster'] = df['cluster'].map(cluster_map) # map 集群到适当的标签 cluster_map = {0: "0", 1: "1", 2: "2", 3:"3", 4:"4", 5: "5"} # 应用映射 df[ '集群'] = df['集群'].map(cluster_map)

My code is as follows:我的代码如下:

import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.decomposition import PCA
sklearn_pca = PCA(n_components = 2)
Y_sklearn = sklearn_pca.fit_transform(X_train_vc.toarray())
kmeans = KMeans(n_clusters=k_clusters, max_iter=600, algorithm = 'auto')
fitted = kmeans.fit(Y_sklearn)
prediction = kmeans.predict(Y_sklearn)

plt.figure(figsize=(12, 6))
plt.scatter(Y_sklearn[:, 0], Y_sklearn[:, 1], c=prediction, s=40, cmap='viridis', linewidths=5)

centers = fitted.cluster_centers_
plt.scatter(centers[:, 0], centers[:, 1],c='black', s=200, alpha=0.6);

在此处输入图像描述

Since you didn't provide data, I'm going to come up with my own.由于您没有提供数据,我将提出自己的数据。

To use a legend, you need to add a scatter plot (with its label) for each cluster.要使用图例,您需要为每个集群添加一个散点图 plot(及其标签)。

In my opinion, rather than putting texts on the figure to indicate the centroids, you should play with the scatter parameters to make it intuitive for people to see that a centroid belongs to a given cluster.在我看来,与其在图形上放置文本来指示质心,不如使用散点参数让人们直观地看出质心属于给定的簇。 Hence, the centroids use a bigger marker, with a quickly identifiable edge color, setting its face color and full opacity.因此,质心使用更大的标记,具有可快速识别的边缘颜色,设置其面部颜色和完全不透明度。 The clusters uses a much lower opacity value.簇使用低得多的不透明度值。

from sklearn.cluster import KMeans
from sklearn.datasets import make_blobs
from itertools import cycle
import matplotlib.pyplot as plt
import matplotlib.cm as cm

n_clusters = 4
X, y_true = make_blobs(n_samples=300, centers=n_clusters,
                       cluster_std=0.60, random_state=0)
kmeans = KMeans(n_clusters=n_clusters)
kmeans.fit(X)
y_kmeans = kmeans.predict(X)
centers = kmeans.cluster_centers_

# map predictions to label
labels = {0: "Cluster 1", 1: "Cluster 2", 2: "Cluster 3", 3: "Cluster 4"}

colors = cycle(cm.tab10.colors)
plt.figure()

for i in range(n_clusters):
    # plot one cluster for each iteration
    color = next(colors)
    # find indeces corresponding to cluser i
    idx = y_kmeans == i
    # plot cluster
    plt.scatter(X[idx, 0], X[idx, 1], color=color, s=50, label=labels[i], alpha=0.25)
    # plot center
    plt.scatter(centers[i, 0], centers[i, 1], edgecolors="k", linewidth=2, color=color, s=200, alpha=1)
plt.legend()

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM