【问题标题】:K-means clustering color not changingK-means 聚类颜色不变
【发布时间】:2017-03-22 15:39:30
【问题描述】:

我用的是jupyter notebook,这里是内核信息

Python 3.5.2 |Anaconda 4.1.1(64 位)| (默认,2016 年 7 月 2 日,17:53:06) [GCC 4.4.7 20120313(红帽 4.4.7-1)]

我正在使用 k-means 聚类。当我聚类时,唯一使用的颜色是蓝色。目前如何设置这不是一个大问题,但我需要按比例放大它,因此颜色需要不同。我遵循了一个教程,所以我不能 100% 理解所有代码。代码如下。

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import style
style.use("ggplot")
from sklearn.cluster import KMeans

x = [1,5,1.5,8,1,9]
y = [2,8,1.8,8,.6,11]

plt.scatter(x,y)
plt.show()

X = np.array([[1,2],[5,8],[1.5,1.8],[8,8],[1,.6],[9,11]])

kmeans = KMeans(n_clusters=2)
kmeans.fit(X)

centroids = kmeans.cluster_centers_
labels = kmeans.labels_

print(centroids)
print(labels)

colors = ['r','b','y','g','c','m']

for i in range(len(X)):
    print("coordinate:",X[i], "label:", labels[i])
    plt.plot(X[i][0], X[i][1], colors[labels[i]], markersize = 10)

plt.scatter(centroids[:, 0],centroids[:, 1], marker = "x", s=150, linewidths = 5, zorder = 10)

plt.show()

plt.scatter(x,y)
plt.scatter(centroids[:, 0],centroids[:, 1], marker = "x", s=150, linewidths = 5, zorder = 10)

plt.show()

我认为我的问题在于这块。

colors = ['r','b','y','g','c','m']

for i in range(len(X)):
    print("coordinate:",X[i], "label:", labels[i])
    plt.plot(X[i][0], X[i][1], colors[labels[i]], markersize = 10)

【问题讨论】:

    标签: python matplotlib k-means jupyter-notebook


    【解决方案1】:

    我确实弄错了。我之前的解决方案不正确。我终于可以好好看看标签和质心的回归了,我认为这应该符合您的要求。

    您可以为 color= 参数提供一个序列作为参数,因此不需要 fol-loop

    colors = ['r','b','y','g','c','m']
    plt.scatter(x,y, color=[colors[l_] for l_ in labels], label=labels)
    plt.scatter(centroids[:, 0],centroids[:, 1], color=[c for c in colors[:len(centroids)]], marker = "x", s=150, linewidths = 5, zorder = 10)
    

    【讨论】:

      【解决方案2】:

      使用 K 表示您希望每个集群具有不同的颜色。如果您有 2 个集群,那么您的模型 kmeans 的标签存储在 kmeans.labels_ 的一个数组中,类似于 [1 1 1 1 0 0 1 0 0 0 1 0 0...]。要使用特定颜色,请在开始所有绘图代码之前遍历此内容并使用列表设置每个点的颜色:

      colors = []
      for i in kmeans.labels_:
        if i == 0:
          colors.append('blue')
        elif i == 1:
          colors.append('orange')
      

      如果您想为您的颜色使用预定义的 Seaborn 调色板,您也可以遍历该调色板!例如,如果您想使用“深”调色板:

      palette = sns.color_palette('deep')
      colors = []
      for i in kmeans.labels_:
        if i == 0:
          colors.append(palette[0])
        elif i == 1:
          colors.append(palette[1])
      

      如果您有 3 种颜色,那么您需要为 i == 2 添加另一个 elif,依此类推。

      然后,当您创建绘图时,只需将 c 参数设置为等于您创建的 colors 列表:

      plt.scatter(df['x'], df['y'], c = colors)
      plt.show()
      

      【讨论】:

        猜你喜欢
        • 2019-06-22
        • 2015-04-11
        • 2018-01-12
        • 2011-08-13
        • 2013-08-08
        • 2013-02-14
        • 2018-01-14
        • 2018-10-12
        • 2012-07-22
        相关资源
        最近更新 更多