【发布时间】: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