【问题标题】:Python 3: how to plot 39 K-means clusters, all with a different color?Python 3:如何绘制 39 个 K-means 簇,所有簇都具有不同的颜色?
【发布时间】:2021-12-08 07:13:45
【问题描述】:

我有一个数据框“hvc_hvposition”,其中包含我客户的位置(经度和纬度)。我想使用 K-means 聚类将这些客户分成 39 个组。然后,我想用散点图绘制这些客户,每个客户都有他们集群的颜色。这是我到目前为止的代码,但我似乎无法弄清楚如何获得正确的颜色。

#K-means clustering of customers
from sklearn.cluster import KMeans
from matplotlib import colors as mcolors
colors = dict(mcolors.BASE_COLORS,**mcolors.CSS4_COLORS)
km_model = KMeans(n_clusters=39)
km_model.fit(hvc_hvposition[["LAT", "LONG"]])
cluster_labels = km_model.predict(hvc_hvposition[["LAT", "LONG"]])

   
# get the cluster color for each customer
cluster_colors = [colors[label] for label in enumerate(cluster_labels)]

# plot the customer locations with the corresponding colors
plt.figure(figsize=(8, 8))
plt.scatter(postcodes_df["LONG"], postcodes_df["LAT"], s=2)
plt.scatter(hvc_hvposition["LAT"], hvc_hvposition["LONG"], color=cluster_colors, s=6)
plt.show()

这是我得到的错误:

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-3-4c87d95de9db> in <module>
     29 
     30 # get the cluster color for each customer
---> 31 cluster_colors = [colors[label] for label in enumerate(cluster_labels)]
     32 
     33 # plot the customer locations with the corresponding colors

<ipython-input-3-4c87d95de9db> in <listcomp>(.0)
     29 
     30 # get the cluster color for each customer
---> 31 cluster_colors = [colors[label] for label in enumerate(cluster_labels)]
     32 
     33 # plot the customer locations with the corresponding colors

KeyError: (0, 15)

我该如何解决这个问题?

【问题讨论】:

  • 你真的希望人们能够在一个情节中区分 39 种颜色吗?我敢打赌那是不可能的。至少使用不同标记和颜色的组合?尽管如此,39 还是很多...... 5 种不同的标记和 8 种颜色听起来更可行。

标签: python python-3.x k-means


【解决方案1】:

我认为您尝试提取字典颜色值的方式是问题所在。而不是cluster_colors = [colors[label] for label in enumerate(cluster_labels)],试试:

col = [c for c in colors.values()]
cluster_colors = [col[index] for index in cluster_labels]

【讨论】:

    猜你喜欢
    • 2023-04-07
    • 2016-12-18
    • 2022-11-05
    • 2021-06-18
    • 2016-12-03
    • 2011-07-24
    • 1970-01-01
    • 2016-04-14
    • 2016-11-26
    相关资源
    最近更新 更多