jxn
jxn

Reputation: 8025

plotting the center of a k-means cluster to be the same color as its cluster points

I am able to plot all the data points of a kmeans cluster such that clusters are denoted by different colors. However now i want to also plot the center of the cluster to be the same color as the cluster points. Here is my code:

num_clusters=5
data2D = pca.transform(X)
centers2D = pca.transform(km.cluster_centers_)
labels=km.labels_
colors=['#000000','#FFFFFF','#FF0000','#00FF00','#0000FF']
col_map=dict(zip(set(labels),colors))
label_color = [col_map[l] for l in labels]
plt.scatter( data2D[:,0], data2D[:,1], c=label_color) # This plots the cluster points.

Now the problem lies here, How do i also plot the cluster center points to be the same color as the points? Specifically, what should i use for c ?

plt.scatter(centers2D[:,0], centers2D[:,1],  marker='x', s=200, linewidths=2, c=label_color)

using c=label_color doesnt work because centers2D is a list of coordinates as shown below. How do i map the coordinates to the same colors i map to integers for the first scatter plot?

print centers2D

[[ 0.03563292 -0.09528218]
 [ 0.05799584  0.01593253]
 [ 0.02265664  0.05109819]
 [ 0.09457535 -0.11127898]
 [-0.16129666  0.00428571]]

Upvotes: 2

Views: 3065

Answers (1)

Matt Hancock
Matt Hancock

Reputation: 4039

label_color is an array matching each data point in data2D to its cluster-color via col_map. You need an array that matches each data point in centers2D to its cluster-color. For instance:

center_colors = [col_map[l] for l in range(num_clusters)]
plt.scatter(centers2D[:,0], centers2D[:,1],  marker='x', s=200, linewidths=2, c=center_colors)

Personally, I think what you're trying to do would be made a tad more simple and clear by just iterating. For instance:

for i in range(num_clusters):
    plt.scatter(data2D[labels==i,0], data2D[labels==i,1], c=colors[i])
    plt.scatter(centers2D[i,0], centers2D[i,1], c=colors[i], marker='x', s=200, linewidths=2)

In this manner, you don't have to use a color map array for your points.

Upvotes: 2

Related Questions