如何将标签放在群集绘制的图像中:在侧面指示它是哪个群集,以及中心?

发布于 2025-01-25 17:11:34 字数 933 浏览 4 评论 0 原文

我使用K表示聚类,并想将标签放在指示颜色的侧面,并在中心介绍哪个集群质心,有人可以帮忙吗?我尝试了解决方案,但没有帮助。谢谢。 我的代码是folows:

我也尝试映射名称,但效果不佳。 #地图簇到适当的标签 cluster_map = {0:“ 0”,1:“ 1”,2:“ 2”,3:“ 3”,4:“ 4”,5:“ 5”} #应用映射 df ['cluster'] = df ['cluster']。map(cluster_map)

我的代码如下:

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);

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? 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 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)

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);

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怪异←思 2025-02-01 17:11:35

由于您没有提供数据,因此我会想出自己的数据。

要使用图例,您需要为每个群集添加一个散点图(带有标签)。

在我看来,您应该使用散点参数来使人们直观地看到质心属于给定的群集,而不是将文本放在图表上以表明质心。因此,质心使用更大的标记,具有快速识别的边缘颜色,设置其面部颜色和完全不透明度。群集使用的不透明度值要低得多。

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()

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.

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()

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文