0
$\begingroup$

I am working with a dataset (X) to predict 12 clusters with K-Means using python SKLEARN library:

numClusters= 12
kmeans = KMeans(n_clusters=numClusters).fit(X)
centroids = kmeans.cluster_centers_

# Predicting the clusters
labels = kmeans.predict(X)
# Getting the cluster centers
C = kmeans.cluster_centers_

#transform n variiables to 2 principal components to plot
pca = PCA(n_components=2)
principalComponents = pca.fit_transform(X)
principalDf = pd.DataFrame(data = principalComponents
         , columns = ['principal component 1', 'principal component 2'])

colors =['red','green','blue','cyan','yellow', 'lime','orange','coral','brown','peru','khaki','tan']
centroidColor= []
for item in range(numClusters):
  centroidColor.append(colors[item])

dataPointColor=[]
for row in labels:
  dataPointColor.append(colors[row])

fig = plt.figure(figsize = (10,10))
ax = fig.add_subplot(1,1,1) 
ax.set_xlabel('Principal Component 1', fontsize = 15)
ax.set_ylabel('Principal Component 2', fontsize = 15)
ax.set_title('2 component PCA', fontsize = 20)
plt.scatter(principalDf['principal component 1'], principalDf['principal component 2'], 
c=dataPointColor, s=50, alpha=0.5)

plt.scatter(C[:, 0], C[:, 1], c=centroidColor, s=200, marker=('x'))
plt.show()

My problem comes when trying to plot the n dimensions of my dataset into 2 PCA. The clustering seems fine, but the centroids (marked with a star icon) are not correct:

enter image description here

The centroids should not always be in the same near spot. I know that I did not standardize the dataset (X) because all values are in a range [1:5]:

[[4 5 4 ... 4 4 4]
[4 4 1 ... 4 3 5]
[3 4 3 ... 1 1 4]
...
[2 5 1 ... 2 1 4]
[5 5 1 ... 5 5 1]
[2 5 1 ... 1 1 5]]

Could the problem come from that?

Thanks for your help.

$\endgroup$
1
$\begingroup$

In your plot you used PCA to reduce the dimensionality of your data, but you plotted the first 2 dimensions of your centroids. You should also transform the centroids using the PCA transform you fitted on your data.

This code should work for you

numClusters= 12
kmeans = KMeans(n_clusters=numClusters).fit(X)
centroids = kmeans.cluster_centers_

# Predicting the clusters
labels = kmeans.predict(X)
# Getting the cluster centers
C = kmeans.cluster_centers_

#transform n variiables to 2 principal components to plot
pca = PCA(n_components=2)
pca_fit = pca.fit(X)
principalComponents = pca_fit.transform(X)
principalDf = pd.DataFrame(data = principalComponents
         , columns = ['principal component 1', 'principal component 2'])

colors =['red','green','blue','cyan','yellow', 'lime','orange','coral','brown','peru','khaki','tan']
centroidColor= []
for item in range(numClusters):
  centroidColor.append(colors[item])

dataPointColor=[]
for row in labels:
  dataPointColor.append(colors[row])

fig = plt.figure(figsize = (10,10))
ax = fig.add_subplot(1,1,1) 
ax.set_xlabel('Principal Component 1', fontsize = 15)
ax.set_ylabel('Principal Component 2', fontsize = 15)
ax.set_title('2 component PCA', fontsize = 20)
plt.scatter(principalDf['principal component 1'], principalDf['principal component 2'], 
c=dataPointColor, s=50, alpha=0.5)

C_transformed = pca_fit.transform(C)
plt.scatter(C_transformed[:, 0], C_transformed[:, 1], c=centroidColor, s=200, marker=('x'))
plt.show()
$\endgroup$
1
  • $\begingroup$ yes, that was the problem. Thanks! $\endgroup$ Jun 23 '20 at 23:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.